文章首发于:clawhub.club
ImageIO:
JDK自带的图片处理类,处理的质量还不错,但是网上都说会发生OOM。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public static void thumbnail(File img, int width, int height, OutputStream out) throws IOException { BufferedImage bufferedImage = ImageIO.read(img); Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = tag.getGraphics(); graphics.setColor(Color.RED); graphics.drawImage(image, 0, 0, null); graphics.dispose(); ImageIO.write(tag, "JPEG", out); }
|
GraphicsMagick:
—java
是个优秀的图片处理库,官网,需要先安装,再使用。
例子:
maven管理依赖
1 2 3 4 5 6
| <dependency> <groupId>org.im4java</groupId> <artifactId>im4java</artifactId> <version>1.4.0</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public static void zoomImage(String imagePath, String newPath, Integer width, Integer height, Double quality) throws InterruptedException, IOException, IM4JavaException { IMOperation op = new IMOperation(); op.addImage(imagePath); if (width == null) { op.resize(null, height); } else if (height == null) { op.resize(width); } else { op.resize(width, height); } op.quality(quality); op.addImage(newPath); ConvertCmd convert = new ConvertCmd(true); if (System.getProperty("os.name").toLowerCase().contains("windows")) { convert.setSearchPath("C:\\Program Files\\GraphicsMagick-1.3.31-Q16"); } convert.run(op); }
|
OpenCV:
—java
这个家伙的名称就比较亮了,公司又专门搞这个的,我只是简单的用用,java中也有工具包直接操作openCV,但是使用起来还是没有C++操作起来快。
maven依赖
1 2 3 4 5 6
| <dependency> <groupId>org.bytedeco.javacpp-presets</groupId> <artifactId>opencv</artifactId> <version>3.4.3-1.4.3</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public static void thumbnail(String srcImagePath, String desImagePath, double scale) { Mat src = Imgcodecs.imread(srcImagePath); Mat dst = src.clone(); Imgproc.resize(src, dst, new Size(src.width() * scale, src.height() * scale)); Imgcodecs.imwrite(desImagePath, dst); }
|
Thumbnails:
这个是谷歌的一个包,使用起来很方便。
maven依赖
1 2 3 4 5
| <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12
|
public static void thumbnail(String srcImagePath, String desImagePath, double scale) throws IOException { Thumbnails.of(srcImagePath).scale(scale).toFile(desImagePath); }
|