java生成缩略图的方法示例
程序员文章站
2024-03-02 23:22:40
本文实例讲述了java生成缩略图的方法。分享给大家供大家参考,具体如下:
package com.util;
import java.awt.image.buf...
本文实例讲述了java生成缩略图的方法。分享给大家供大家参考,具体如下:
package com.util; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; /** * 生成压缩图 * */ public class imagescale { private int width; private int height; private int scalewidth; double support = (double) 3.0; double pi = (double) 3.14159265358978; double[] contrib; double[] normcontrib; double[] tmpcontrib; int startcontrib, stopcontrib; int ndots; int nhalfdots; /** * start: use lanczos filter to replace the original algorithm for image * scaling. lanczos improves quality of the scaled image modify by :blade */ public bufferedimage imagezoomout(bufferedimage srcbufferimage, int w, int h) { width = srcbufferimage.getwidth(); height = srcbufferimage.getheight(); scalewidth = w; if (determineresultsize(w, h) == 1) { return srcbufferimage; } calcontrib(); bufferedimage pbout = horizontalfiltering(srcbufferimage, w); bufferedimage pbfinalout = verticalfiltering(pbout, h); return pbfinalout; } /** * 决定图像尺寸 */ private int determineresultsize(int w, int h) { double scaleh, scalev; // update by libra double wt = w > width ? width : w; double ht = h > height ? height : h; scaleh = (double) wt / (double) width; scalev = (double) ht / (double) height; // 需要判断一下scaleh,scalev,不做放大操作 if (scaleh >= 1.0 && scalev >= 1.0) { return 1; } return 0; } // end of determineresultsize() private double lanczos(int i, int inwidth, int outwidth, double support) { double x; x = (double) i * (double) outwidth / (double) inwidth; return math.sin(x * pi) / (x * pi) * math.sin(x * pi / support) / (x * pi / support); } // end of lanczos() // // assumption: same horizontal and vertical scaling factor // private void calcontrib() { nhalfdots = (int) ((double) width * support / (double) scalewidth); ndots = nhalfdots * 2 + 1; try { contrib = new double[ndots]; normcontrib = new double[ndots]; tmpcontrib = new double[ndots]; } catch (exception e) { system.out.println("init contrib,normcontrib,tmpcontrib" + e); } int center = nhalfdots; contrib[center] = 1.0; double weight = 0.0; int i = 0; for (i = 1; i <= center; i++) { contrib[center + i] = lanczos(i, width, scalewidth, support); weight += contrib[center + i]; } for (i = center - 1; i >= 0; i--) { contrib[i] = contrib[center * 2 - i]; } weight = weight * 2 + 1.0; for (i = 0; i <= center; i++) { normcontrib[i] = contrib[i] / weight; } for (i = center + 1; i < ndots; i++) { normcontrib[i] = normcontrib[center * 2 - i]; } } // end of calcontrib() // 处理边缘 private void caltempcontrib(int start, int stop) { double weight = 0; int i = 0; for (i = start; i <= stop; i++) { weight += contrib[i]; } for (i = start; i <= stop; i++) { tmpcontrib[i] = contrib[i] / weight; } } // end of caltempcontrib() private int getredvalue(int rgbvalue) { int temp = rgbvalue & 0x00ff0000; return temp >> 16; } private int getgreenvalue(int rgbvalue) { int temp = rgbvalue & 0x0000ff00; return temp >> 8; } private int getbluevalue(int rgbvalue) { return rgbvalue & 0x000000ff; } private int comrgb(int redvalue, int greenvalue, int bluevalue) { return (redvalue << 16) + (greenvalue << 8) + bluevalue; } // 行水平滤波 private int horizontalfilter(bufferedimage bufimg, int startx, int stopx, int start, int stop, int y, double[] pcontrib) { double valuered = 0.0; double valuegreen = 0.0; double valueblue = 0.0; int valuergb = 0; int i, j; for (i = startx, j = start; i <= stopx; i++, j++) { valuergb = bufimg.getrgb(i, y); valuered += getredvalue(valuergb) * pcontrib[j]; valuegreen += getgreenvalue(valuergb) * pcontrib[j]; valueblue += getbluevalue(valuergb) * pcontrib[j]; } valuergb = comrgb(clip((int) valuered), clip((int) valuegreen), clip((int) valueblue)); return valuergb; } // end of horizontalfilter() // 图片水平滤波 private bufferedimage horizontalfiltering(bufferedimage bufimage, int ioutw) { int dwinw = bufimage.getwidth(); int dwinh = bufimage.getheight(); int value = 0; bufferedimage pbout = new bufferedimage(ioutw, dwinh, bufferedimage.type_int_rgb); for (int x = 0; x < ioutw; x++) { int startx; int start; int x = (int) (((double) x) * ((double) dwinw) / ((double) ioutw) + 0.5); int y = 0; startx = x - nhalfdots; if (startx < 0) { startx = 0; start = nhalfdots - x; } else { start = 0; } int stop; int stopx = x + nhalfdots; if (stopx > (dwinw - 1)) { stopx = dwinw - 1; stop = nhalfdots + (dwinw - 1 - x); } else { stop = nhalfdots * 2; } if (start > 0 || stop < ndots - 1) { caltempcontrib(start, stop); for (y = 0; y < dwinh; y++) { value = horizontalfilter(bufimage, startx, stopx, start, stop, y, tmpcontrib); pbout.setrgb(x, y, value); } } else { for (y = 0; y < dwinh; y++) { value = horizontalfilter(bufimage, startx, stopx, start, stop, y, normcontrib); pbout.setrgb(x, y, value); } } } return pbout; } // end of horizontalfiltering() private int verticalfilter(bufferedimage pbinimage, int starty, int stopy, int start, int stop, int x, double[] pcontrib) { double valuered = 0.0; double valuegreen = 0.0; double valueblue = 0.0; int valuergb = 0; int i, j; for (i = starty, j = start; i <= stopy; i++, j++) { valuergb = pbinimage.getrgb(x, i); valuered += getredvalue(valuergb) * pcontrib[j]; valuegreen += getgreenvalue(valuergb) * pcontrib[j]; valueblue += getbluevalue(valuergb) * pcontrib[j]; // system.out.println(valuered+"->"+clip((int)valuered)+"<-"); // // system.out.println(valuegreen+"->"+clip((int)valuegreen)+"<-"); // system.out.println(valueblue+"->"+clip((int)valueblue)+"<-"+"-->"); } valuergb = comrgb(clip((int) valuered), clip((int) valuegreen), clip((int) valueblue)); // system.out.println(valuergb); return valuergb; } // end of verticalfilter() private bufferedimage verticalfiltering(bufferedimage pbimage, int iouth) { int iw = pbimage.getwidth(); int ih = pbimage.getheight(); int value = 0; bufferedimage pbout = new bufferedimage(iw, iouth, bufferedimage.type_int_rgb); for (int y = 0; y < iouth; y++) { int starty; int start; int y = (int) (((double) y) * ((double) ih) / ((double) iouth) + 0.5); starty = y - nhalfdots; if (starty < 0) { starty = 0; start = nhalfdots - y; } else { start = 0; } int stop; int stopy = y + nhalfdots; if (stopy > (int) (ih - 1)) { stopy = ih - 1; stop = nhalfdots + (ih - 1 - y); } else { stop = nhalfdots * 2; } if (start > 0 || stop < ndots - 1) { caltempcontrib(start, stop); for (int x = 0; x < iw; x++) { value = verticalfilter(pbimage, starty, stopy, start, stop, x, tmpcontrib); pbout.setrgb(x, y, value); } } else { for (int x = 0; x < iw; x++) { value = verticalfilter(pbimage, starty, stopy, start, stop, x, normcontrib); pbout.setrgb(x, y, value); } } } return pbout; } // end of verticalfiltering() int clip(int x) { if (x < 0) return 0; if (x > 255) return 255; return x; } /** * end: use lanczos filter to replace the original algorithm for image * scaling. lanczos improves quality of the scaled image modify by :blade */ public boolean scale(string source, string target, int width, int height) { file f = new file(source); try { bufferedimage bi = imageio.read(f); bufferedimage out = null; imagescale scal = new imagescale(); int _width = bi.getwidth();// add int _height = bi.getheight();// add int[] _arr = this.getimagewidthandheight(_width, _height, width, height);// add // out = scal.imagezoomout(bi, width, height); out = scal.imagezoomout(bi, _arr[0], _arr[1]); file t = new file(target); imageio.write(out, "jpg", t); return true; } catch (ioexception e) { e.printstacktrace(); return false; } } /** * 得到放大或者缩小后的比例 * * @param w * 图片原宽 * @param h * 原高 * @param tarw * 转换后的宽 * @param zoom * 放大还是缩小 * @return 返回宽和高的数组 */ private static int[] getimagewidthandheight(int orgw, int orgh, int avw, int avh) { int width = 0; int height = 0; if (orgw > 0 && orgh > 0) { if (orgw / orgh >= avw / avh) { if (orgw > avw) { width = avw; height = (orgh * avw) / orgw; } else { width = orgw; height = orgh; } system.out.println("++widht:" + width + " height" + height); } else { if (orgh > avh) { height = avh; width = (orgw * avh) / orgh; } else { width = orgw; height = orgh; } system.out.println("++widht:" + width + " height" + height); } } int[] arr = new int[2]; arr[0] = width; arr[1] = height; // long start = system.currenttimemillis(); // int width = 0; // int height = 0; // if ((w / tarw) >= (h / tarh)) {// 宽的缩小比例大于高的 // width = tarw; // height = h * tarw / w; // system.out.println(width + " " + height); // } else { // height = tarh; // width = w * tarh / h; // system.out.println(width + " " + height); // } // int[] arr = new int[2]; // arr[0] = width; // arr[1] = height; // long end = system.currenttimemillis(); // system.out.println("宽高处理:" + (end - start)); return arr; } public void picscale(string source, string target, int w, int h) { file f = new file(source); int width = 0; int height = 0; try { bufferedimage bi = imageio.read(f); int[] arr = getimagewidthandheight(bi.getwidth(), bi.getheight(), w, h); width = arr[0]; height = arr[1]; bufferedimage out = null; imagescale scal = new imagescale(); out = scal.imagezoomout(bi, width, height); file t = new file(target); imageio.write(out, "jpg", t); } catch (ioexception e) { e.printstacktrace(); } } /** * *调用scale(源文件路径,保存路径,最大宽,最大高) * * */ public static void main(string[] args) { imagescale is = new imagescale(); long start = system.currenttimemillis(); is.scale("d:/nie.jpg", "d:/t6.jpg", 250, 194); long end = system.currenttimemillis(); system.out.println("时间:" + (end - start)); } }
ps:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:
在线图片转换base64工具:
ico图标在线生成工具:
在线email邮箱图标制作工具:
在线图片格式转换(jpg/bmp/gif/png)工具:
更多java相关内容感兴趣的读者可查看本站专题:《java图片操作技巧汇总》、《java日期与时间操作技巧汇总》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》及《java数据结构与算法教程》。
希望本文所述对大家java程序设计有所帮助。