java对图片进行操作,仅仅是小demo
package com.cy.thumb; import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator; import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream; import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; public class Thumb {
public static void main(String[] args) throws IOException { //把图片image.png 长宽等比缩小5倍。
reduceImage("E:/image.png", "E:/image1.png", 5); //把图片image.png 长宽各设置为100
reduceImage("E:/image.png", "E:/image2.png", 100, 100); } /**
* 对图片进行剪裁 返回字节数组
* @param is 图片输入流
* @param width 裁剪图片的宽
* @param height 裁剪图片的高
* @param imageFormat 输出图片的格式 "jpeg jpg等"
* @return
*/
public static byte[] clipImage(InputStream is,int width, int height, String imageFormat){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
// 构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(is);
// 缩小边长
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
ImageIO.write(tag, imageFormat, bos);
} catch (IOException e) {
e.printStackTrace();
} return bos.toByteArray();
} /**
* 重置图片大小
* @param srcImagePath 读取图片路径
* @param toImagePath 写入图片路径
* @param width 重新设置图片的宽
* @param height 重新设置图片的高
* @throws IOException
*/
public static void reduceImage(String srcImagePath,String toImagePath,int width, int height) throws IOException{
FileOutputStream out = null;
try{
//读入文件
File file = new File(srcImagePath);
// 构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
// 缩小边长
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
} /**
* 按倍率缩小图片
* @param srcImagePath 读取图片路径
* @param toImagePath 写入图片路径
* @param ratio 缩小比率 宽、高一起等比率缩小
* @throws IOException
*/
public static void reduceImage(String srcImagePath,String toImagePath,int ratio) throws IOException{
FileOutputStream out = null;
try{
//读入文件
File file = new File(srcImagePath);
// 构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
// 缩小边长
BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB);
// 绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
} /**
* 对图片裁剪,并把裁剪新图片保存
* @param srcPath 读取源图片路径
* @param toPath 写入图片路径
* @param x 剪切起始点x坐标
* @param y 剪切起始点y坐标
* @param width 剪切宽度
* @param height 剪切高度
* @param readImageFormat 读取图片格式
* @param writeImageFormat 写入图片格式
*/
public static void cropImage(String srcPath, String toPath, int x,int y,int width,int height, String readImageFormat,String writeImageFormat){
FileInputStream fis = null ;
ImageInputStream iis =null ;
try{
//读取图片文件
fis = new FileInputStream(srcPath);
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(readImageFormat);
ImageReader reader = readers.next();
//获取图片流
iis = ImageIO.createImageInputStream(fis);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
//定义一个矩形
Rectangle rect = new Rectangle(x, y, width, height);
//提供一个 BufferedImage,将其用作解码像素数据的目标。
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
//保存新图片
ImageIO.write(bi, writeImageFormat, new File(toPath));
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(fis!=null){
fis.close();
}
if(iis!=null){
iis.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
} }
上面部分参考文章:http://blog.csdn.net/u012486840/article/details/52937823
下面转载自:http://blog.csdn.net/u012481520/article/details/51802469#t0
int width = 100;
int height = 100;
// 1.创建一个不带透明色的BufferedImage对象
BufferedImage bimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); // 2.创建一个带透明色的BufferedImage对象
bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // 3.创建一个与屏幕相适应的BufferedImage对象
。。。。 。。。。 四、BufferedImage —->byte[]
ImageIO.write(BufferedImage image,String format,OutputStream out);方法可以很好的解决问题;
参数image表示获得的BufferedImage;
参数format表示图片的格式,比如“gif”等;
参数out表示输出流,如果要转成Byte数组,则输出流为ByteArrayOutputStream即可;
执行完后,只需要toByteArray()就能得到byte[]; 五、byte[] ——>BufferedImage
ByteArrayInputStream in = new ByteArrayInputStream(byte[]b); //将b作为输入流;
BufferedImage image = ImageIO.read(InputStream in); //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
java对图片进行操作,仅仅是小demo的更多相关文章
- Java多线程同步问题:一个小Demo完全搞懂
		版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.一个简单的Demo引发的血案 关于线程同步问题我们从一个 ... 
- java线程间通信:一个小Demo完全搞懂
		版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ... 
- spark集群配置以及java操作spark小demo
		spark 安装 配置 使用java来操作spark spark 安装 tar -zxvf spark-2.4.0-bin-hadoop2.7.tgz rm spark-2.4.0-bin-hadoo ... 
- 【Java】Jsoup爬虫,一个简单获取京东商品信息的小Demo
		简单记录 - Jsoup爬虫入门实战 数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫! 爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 我们经常需要分析HTML网 ... 
- 聊聊UDP、TCP和实现一个简单的JAVA UDP小Demo
		最近真的比较忙,很久就想写了,可是一直苦于写点什么,今天脑袋灵光一闪,觉得自己再UDP方面还有些不了解的地方,所以要给自己扫盲. 好了,咱们进入今天的主题,先列一下提纲: 1. UDP是什么,UDP适 ... 
- OMG,12 个精致的 Java 字符串操作小技巧,学它
		字符串可以说是 Java 中最具有代表性的类了,似乎没有之一哈,这就好像直播界的李佳琪,脱口秀中的李诞,一等一的大哥地位.不得不承认,最近吐槽大会刷多了,脑子里全是那些段子,写文章都有点不由自主,真的 ... 
- 基于BaseAdapter的Listview小Demo
		ListView是android开发中比较常用的控件, 其中适配器模式可以选择: ArrayAdapter:简单易用,通常用于将数组或者List集合的读个包值封装成多个列表项 SimpleAdapte ... 
- 在JAVA中记录日志的十个小建议
		JAVA日志管理既是一门科学,又是一门艺术.科学的部分是指了解写日志的工具以及其API,而选择日志的格式,消息的格式,日志记录的内容,哪种消息对应于哪一种日志级别,则完全是基于经验.从过去的实践证明, ... 
- Android -BLE蓝牙小DEMO
		代码地址如下:http://www.demodashi.com/demo/13890.html 原文地址: https://blog.csdn.net/vnanyesheshou/article/de ... 
随机推荐
- Linux清除Windows密码
			下载安装ntfs-3g 下载驱动让linux挂载windows磁盘 https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2017.3.23.tgz 安装 t ... 
- 框架-spring入门总结
			框架-spring入门总结 参考: http://www.cnblogs.com/heavenyes/p/3908546.html http://www.cnblogs.com/heavenyes/p ... 
- Today's harvest !!!
			今天将Mybatis的视频看到了第60集,其之前讲解了自表的主外键查询.例如一个新闻表中,有一级栏目,二级栏目,三级栏目,其中二级栏目的pid为一级栏目的id,如此种种. 而今天做的小项目中使用了 e ... 
- Mac下安装hexo Error: Cannot find module './build/Release/DTraceProviderBindings 解决
			参考: Github:Mac 下已经装了hexo,仍旧报错 官方文档 $ npm install hexo --no-optional if it doesn't work try $ npm uni ... 
- 使用javascript模拟常见数据结构(三)
			六.字典和散列表 我们已经知道,集合表示一组互不相同的元素(不重复元素).在字典中,存储的是键值对,其中键值是用来查询特定的元素的.字典和集合是很相似的,集合采用[值,值]的方式存储,而字典则是以[键 ... 
- Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学
			C. Vasya and Petya's Game time limit per test 1 second memory limit per test 256 megabytes input sta ... 
- 安装 bochs
			sudo apt-get install bochs 以后接着安装bochs-x 
- hybird app项目实例:安卓webview中HTML5拍照图片上传
			应用的平台环境:安卓webview: 涉及的技术点: (1) <input type="file" > :在开发中,安卓webview默认点击无法调用文件选择与相机拍照 ... 
- Leetcode 18
			class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int ta ... 
- restframework api(基础3CBV)
			一 CBV源码流程 urls.py from django.conf.urls import url from django.contrib import admin from app01 impor ... 
