代码实现如下:
第一个开源: Thumbnailator
long begin = System.currentTimeMillis();
try {
Thumbnails.of(new File("E:\\test\\photos\\亲密敌人.jpg"))
.size(300, 200)
//.rotate(90)
.outputQuality(0.8f)
.toFile(new File("E:\\000.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end - begin);
第二个开源: java-image-scaling
long begin2 = System.currentTimeMillis();
File fileOne = new File("E:\\test\\photos\\阿凡达.jpg");
File hd_ad = new File("E:\\test\\data\\高清页面-广告图片.jpg");
try {
BufferedImage bufferedImage11 = new ThumpnailRescaleOp(1104,540).doFilter(ImageIO.read(fileOne),null,1104, 540);
ImageIO.write(bufferedImage11, "jpg", hd_ad);
} catch (IOException e) {
e.printStackTrace();
}
long end2 = System.currentTimeMillis();
System.out.println(end2-begin2);
第三个开源: EasyImage
long begin3 = System.currentTimeMillis();
Image image2 = new Image("E:\\test\\photos\\亲密敌人.jpg");
image2.resize(290, 400);
image2.saveAs("e:\\222.jpg");
long end3 = System.currentTimeMillis();
System.out.println(end3 - begin3);
//融合两张图片
Image image3 = new Image("E:\\test\\photos\\亲密敌人.jpg");
image3.combineWithPicture("E:\\test\\photos\\泰坦尼克号.jpg");
image3.saveAs("E:\\333.jpg");
//强调图像的某个部分:
Image image4 = new Image("E:\\test\\photos\\亲密敌人.jpg");
image4.emphasize(250, 200, 2300, 500);
image4.saveAs("E:\\444.jpg");
java-image-scaling图片失真问题解决方案:
原因:图片本身的问题(ICC丢失或不全)
public static BufferedImage toBufferedImage(File img) throws IOException {
Image image = Toolkit.getDefaultToolkit().getImage(img.getPath());
BufferedImage bimage = null;
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
image = new ImageIcon(image).getImage();
int width = image.getWidth(null);
int height = image.getHeight(null);
if (width < 0 || height < 0) {
/*Map<String,Integer> map = getImageSize(img);
width = map.get(IMAGE_WIDTH);
height = map.get(IMAGE_HEIGHT);*/
return null;
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(width, height, transparency);
} catch (HeadlessException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
调用:
File fileOne = new File("E:\\test\\photos\\test.jpg");
BufferedImage bufferedImageTmp2 = null;
try {
bufferedImageTmp2 = toBufferedImage(fileOne);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedImage bufferedImage1 = new ThumpnailRescaleOp(470,300).doFilter(bufferedImageTmp2,null,470, 300);