您好,欢迎光临本网站![请登录][注册会员]  
文件名称: java缩略图
  所属分类: 其它
  开发工具:
  文件大小: 3kb
  下载次数: 0
  上传时间: 2018-03-30
  提 供 者: qq_40******
 详细说明: 引用包后调用包就好了 package image; import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) { long start = System.currentTimeMillis(); try { ImageUtil.resizeFix(new File("D:\\qqͼƬ\\1111.jpg"), new File("D:\\qqͼƬ\\99999.jpg"), 100, 100); long end = System.currentTimeMillis(); System.out.println("success:" + (end - start)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } jar包的源码 package image; /** * 图片缩小算法,方形区域抽样 */ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; public class ImageUtil { private int width; private int height; private int zoomWidth; private int zoomHeight; private File destFile; private BufferedImage srcBufferImage; public static void resizeFix(File srcFile, File destFile, int width, int height) throws IOException { new ImageUtil(srcFile, destFile, width, height); } public static void resizeFix(BufferedImage bufImg, File destFile, int width, int height) throws IOException { new ImageUtil(bufImg, destFile, width, height); } protected ImageUtil(File srcFile, File destFile, int zoomWidth, int zoomHeight) throws IOException { this.destFile = destFile; this.zoomWidth = zoomWidth; this.zoomHeight = zoomHeight; this.srcBufferImage = javax.imageio.ImageIO.read(srcFile); this.width = this.srcBufferImage.getWidth(); this.height = this.srcBufferImage.getHeight(); if (width <= zoomWidth && height <= zoomHeight) { FileUtils.copyFile(srcFile, destFile); } else { resizeFix(); } } protected ImageUtil(BufferedImage srcBufferImage, File destFile, int zoomWidth, int zoomHeight) throws IOException { this.destFile = destFile; this.zoomWidth = zoomWidth; this.zoomHeight = zoomHeight; this.srcBufferImage = srcBufferImage; this.width = this.srcBufferImage.getWidth(); this.height = this.srcBufferImage.getHeight(); resizeFix(); } /** * 压缩图片 * * @throws IOException */ protected void resizeFix() throws IOException { if (width <= zoomWidth && height <= zoomHeight) { resize(width, height); } else if ((float) width / height > (float) zoomWidth / zoomHeight) { resize(zoomWidth, Math.round((float) zoomWidth * height / width)); } else { resize(Math.round((float) zoomHeight * width / height), zoomHeight); } } private void resize(int w, int h) throws IOException { BufferedImage imgBuf = scaleImage(w, h); File parent = destFile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } ImageIO.write(imgBuf, "jpeg", destFile); } private BufferedImage scaleImage(int outWidth, int outHeight) { int[] rgbArray = srcBufferImage.getRGB(0, 0, width, height, null, 0, width); BufferedImage pbFinalOut = new BufferedImage(outWidth, outHeight, BufferedImage.TYPE_INT_RGB); double hScale = ((double) width) / ((double) outWidth);// 宽缩小的倍数 double vScale = ((double) height) / ((double) outHeight);// 高缩小的倍数 int winX0, winY0, winX1, winY1; int valueRGB = 0; long R, G, B; int x, y, i, j; int n; for (y = 0; y < outHeight; y++) { winY0 = (int) (y * vScale + 0.5);// 得到原图高的Y坐标 if (winY0 < 0) { winY0 = 0; } winY1 = (int) (winY0 + vScale + 0.5); if (winY1 > height) { winY1 = height; } for (x = 0; x < outWidth; x++) { winX0 = (int) (x * hScale + 0.5); if (winX0 < 0) { winX0 = 0; } winX1 = (int) (winX0 + hScale + 0.5); if (winX1 > width) { winX1 = width; } R = 0; G = 0; B = 0; for (i = winX0; i < winX1; i++) { for (j = winY0; j < winY1; j++) { valueRGB = rgbArray[width * j + i]; R += getRedValue(valueRGB); G += getGreenValue(valueRGB); B += getBlueValue(valueRGB); } } n = (winX1 - winX0) * (winY1 - winY0); R = (int) (((double) R) / n + 0.5); G = (int) (((double) G) / n + 0.5); B = (int) (((double) B) / n + 0.5); valueRGB = comRGB(clip((int) R), clip((int) G), clip((int) B)); pbFinalOut.setRGB(x, y, valueRGB); } } return pbFinalOut; } private int clip(int x) { if (x < 0) return 0; if (x > 255) return 255; return x; } 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; } } 所需要的驱动包 commons-io-1.3.2.jar commons-io-1.3.2.jar + 上面的代码 = 你下载的jar ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 缩略图
 输入关键字,在本站1000多万海量源码库中尽情搜索: