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 ...
随机推荐
- CF_400_D
codeforces_400_D 题目大意:给出n扇门,m把钥匙,和没把钥匙可以改变状态(关->开,开->关>)的门的数量及对应编号(保证每个门被两把钥匙控制),现给出n扇门的初始状 ...
- linux中find与rm实现查找并删除目录或文件
linux 下用find命令查找文件,rm命令删除文件. 删除指定目录下指定文件find 要查找的目录名 -name .svn |xargs rm -rf 删除指定名称的文件或文件夹: find -t ...
- Python网络编程(Sockets)
一个简单的服务器 #!/usr/bin/python3 # This is server.py file import socket # create a socket object serverso ...
- 快递100API接口调用代码示例
package com.util; import java.io.IOException; import java.io.InputStream; import java.net.MalformedU ...
- 【Python】远离 Python 最差实践,避免挖坑
原文链接:http://blog.guoyb.com/2016/12/03/bad-py-style/ 最近在看一些陈年老系统,其中有一些不好的代码习惯遗留下来的坑:加上最近自己也写了一段烂代码导致服 ...
- ubuntu 10.04 安装arm交叉编译器
家里有一台cotext-A9(armv7-a) 的盒子,现在不用了, 一直想着废物利用.于是想怎么为这盒子编译程序. 目标机器: root@routon-h1:/# uname -a Linux ro ...
- UVALive-3126 Taxi Cab Scheme (DAG的最小路径覆盖)
题目大意:要给n个人安排车,已知每个人的出发时间和起点与终点,问最少需要安排几辆车才能完成任务. 题目分析:最小路径覆盖.如果送完a到目的地后能在b出发之前赶来接b,那么连一条有向边a->b,最 ...
- Fly Vim, First-Class
http://corner.squareup.com/2013/08/fly-vim-first-class.html Engineers at Square use a wide variety o ...
- Eclipse Oxygen SVN Connector Setup
新版的Eclipse(Oxygen)安装完Subversive后,现时无法自动安装SVN Connector,无论选择哪个都会自动关闭. 解决方法: Help -> Install New So ...
- 2019年微信小程序1月TOP100榜单