Java实现的图片高质量缩放类定义与用法示例
程序员文章站
2024-02-26 19:06:40
本文实例讲述了java实现的图片高质量缩放类定义与用法。分享给大家供大家参考,具体如下:
找了很多都不理想,最后找个到老外写的,不得不承认老外写的确实牛b。
p...
本文实例讲述了java实现的图片高质量缩放类定义与用法。分享给大家供大家参考,具体如下:
找了很多都不理想,最后找个到老外写的,不得不承认老外写的确实牛b。
package com.test; import com.sun.image.codec.jpeg.jpegimageencoder; import com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegencodeparam; import javax.swing.*; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.awt.*; import java.awt.image.bufferedimage; import java.awt.image.kernel; import java.awt.image.convolveop; public class imageutil { public static void resize(file originalfile, file resizedfile, int newwidth, float quality) throws ioexception { if (quality > 1) { throw new illegalargumentexception( "quality has to be between 0 and 1"); } imageicon ii = new imageicon(originalfile.getcanonicalpath()); image i = ii.getimage(); image resizedimage = null; int iwidth = i.getwidth(null); int iheight = i.getheight(null); if (iwidth > iheight) { resizedimage = i.getscaledinstance(newwidth, (newwidth * iheight) / iwidth, image.scale_smooth); } else { resizedimage = i.getscaledinstance((newwidth * iwidth) / iheight, newwidth, image.scale_smooth); } // this code ensures that all the pixels in the image are loaded. image temp = new imageicon(resizedimage).getimage(); // create the buffered image. bufferedimage bufferedimage = new bufferedimage(temp.getwidth(null), temp.getheight(null), bufferedimage.type_int_rgb); // copy image to buffered image. graphics g = bufferedimage.creategraphics(); // clear background and paint the image. g.setcolor(color.white); g.fillrect(0, 0, temp.getwidth(null), temp.getheight(null)); g.drawimage(temp, 0, 0, null); g.dispose(); // soften. float softenfactor = 0.05f; float[] softenarray = { 0, softenfactor, 0, softenfactor, 1 - (softenfactor * 4), softenfactor, 0, softenfactor, 0 }; kernel kernel = new kernel(3, 3, softenarray); convolveop cop = new convolveop(kernel, convolveop.edge_no_op, null); bufferedimage = cop.filter(bufferedimage, null); // write the jpeg to a file. fileoutputstream out = new fileoutputstream(resizedfile); // encodes image as a jpeg data stream jpegimageencoder encoder = jpegcodec.createjpegencoder(out); jpegencodeparam param = encoder .getdefaultjpegencodeparam(bufferedimage); param.setquality(quality, true); encoder.setjpegencodeparam(param); encoder.encode(bufferedimage); } // example usage public static void main(string[] args) throws ioexception { // file originalimage = new file("c:\\11.jpg"); // resize(originalimage, new file("c:\\11-0.jpg"),150, 0.7f); // resize(originalimage, new file("c:\\11-1.jpg"),150, 1f); file originalimage = new file("c:\\1207.gif"); resize(originalimage, new file("c:\\1207-0.jpg"),150, 0.7f); resize(originalimage, new file("c:\\1207-1.jpg"),150, 1f); } }
更多java相关内容感兴趣的读者可查看本站专题:《java图片操作技巧汇总》、《java日期与时间操作技巧汇总》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》及《java数据结构与算法教程》。
希望本文所述对大家java程序设计有所帮助。
上一篇: ExtJS6.2开发-用户管理(二)
下一篇: Python lambda表达式用法