对图片的操作,按比例缩放,固定尺寸缩放,按尺寸等比缩放,旋转,加水印,压缩图片质量,解决jdk原生对图片编码的兼容问题。
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream("D:\\11.jpg");
System.out.println("输入数据量in="+in.available());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[];
while(true) {
int offset = -1;
try {
offset = in.read(buffer);
}catch(EOFException ex){
ex.printStackTrace();
}
if(offset!=-1){
baos.write(buffer, 0, offset);
}else{
break;
}
}
baos.flush();
InputStream stream1 = new ByteArrayInputStream(baos.toByteArray());
BufferedImage bis = ImageIO.read(stream1);
}
错误信息
输入数据量in=1250482
Exception in thread "main" javax.imageio.IIOException: Unsupported Image Type
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1079)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1050)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1352)
at com.example.demo.latitude.util.ImageUtil.main(ImageUtil.java:35)
Process finished with exit code 1
错误原因
javax.imageio.IIOException: Unsupported Image Type
一般是由于图片颜色模式是CMYK导致的,因为其内部读取文件的类JPEGImageReader只能读取RGB color mode,一般经过ps的图片自动保存曾jpeg格式颜色空间都会改变成CMYK,如果强行转换颜色空间会导致图片失色,gift的图色彩模式是CMYK模式(注意,这是给印刷机用的)在java.awt包下的Image类只能使用RGB模式。可以添加twelvemonkeys的jar即可解决报错,jdk的ImageIO有自动发现功能,会自动查找相关的编解码类并使用,而不使用jdk默认的编解码类,因此代码也不用修改,很方便。
需要的jar
com.twelvemonkeys.imageio
imageio-jpeg
3.6
com.twelvemonkeys.imageio
imageio-tiff
3.6
com.twelvemonkeys.servlet
servlet
3.6
图片缩略图
String newImagePath = "C:\\Users\\23892\\Desktop\\2.jpg";
String srcImagePath = "C:\\Users\\23892\\Desktop\\2.png";
Thumbnails.of(srcImagePath).size(200, 300).toFile(newImagePath);
指定比例缩略图
String newImagePath = "C:\\Users\\23892\\Desktop\\2.jpg";
String srcImagePath = "C:\\Users\\23892\\Desktop\\2.png";
Thumbnails.of(srcImagePath).scale(0.9f).toFile(newImagePath);
对图片进行旋转
String newImagePath = "C:\\Users\\23892\\Desktop\\2.jpg";
String srcImagePath = "C:\\Users\\23892\\Desktop\\2.png";
Thumbnails.of(srcImagePath).scale(1.0f).rotate(180).toFile(newImagePath);
对图片进行格式转换
String newImagePath = "C:\\Users\\23892\\Desktop\\2.jpg";
String srcImagePath = "C:\\Users\\23892\\Desktop\\2.png";
Thumbnails.of(srcImagePath).scale(1.0f).outputFormat("jpg").toFile(newImagePath);
对图片进行剪裁
String newImagePath = "C:\\Users\\23892\\Desktop\\2.jpg";
String srcImagePath = "C:\\Users\\23892\\Desktop\\2.png";
Thumbnails.of(srcImagePath).scale(1.0f).sourceRegion(Positions.CENTER, 400, 400).toFile(newImagePath);
对图片进行加水印操作
String newImagePath = "C:\\Users\\23892\\Desktop\\2.jpg";
String srcImagePath = "C:\\Users\\23892\\Desktop\\2.png";
String warterImagePath = "C:\\Users\\23892\\Desktop\\3.png";
Thumbnails.of(srcImagePath).scale(1.0f).watermark(Positions.CENTER, ImageIO.read(new File(warterImagePath)), 0.25f).toFile(newImagePath);
对图片进行加文字水印操作
String newImagePath = "C:\\Users\\23892\\Desktop\\2.jpg";
String srcImagePath = "C:\\Users\\23892\\Desktop\\2.png";
BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setColor(Color.LIGHT_GRAY);
g.drawRect(0, 0, 10, 10);
char[] data = "lonsun".toCharArray();
g.drawChars(data, 0, data.length, 5, 32);
Thumbnails.of(srcImagePath).scale(1.0f).watermark(Positions.CENTER, bi, 1.0f).toFile(newImagePath);
需要的jar
net.coobird
thumbnailator
0.4.8