把做工程过程比较好的一些内容备份一下,如下内容段是关于 java生成缩略图类的内容。

package com.whatycms.common.util;

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 ImageScale { public static void resizeFix(File srcFile, File destFile, int width, int height) throws IOException { new ImageScale(srcFile, destFile, width, height); }

public static void resizeFix(BufferedImage bufImg, File destFile, int width, int height) throws IOException {new ImageScale(bufImg, destFile, width, height);
}private int width;
private int height;
private int zoomWidth;
private int zoomHeight;
private File destFile;
private BufferedImage srcBufferImage;protected ImageScale(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 ImageScale(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();
}protected void resizeFix() throws IOException {if (width <= zoomWidth && height <= zoomHeight) {resize(width, height);} else if ((float) width / height > (float) zoomWidth / zoomHeight) {} else {}
}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);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++) {if (winY0 < 0) {winY0 = 0;}winY1 = (int) (winY0 + vScale + 0.5);if (winY1 > height) {winY1 = height;}for (x = 0; x < outWidth; x++) {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++) {R += getRedValue(valueRGB);G += getGreenValue(valueRGB);B += getBlueValue(valueRGB);}}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;
}public static void main(String[] args) throws IOException {long start = System.currentTimeMillis();ImageScale.resizeFix(new File("D:/pictures/学会忘记.bmp"), new File("D:/pictures/学会忘记2.bmp"), 60, 60);long end = System.currentTimeMillis();System.out.println("success:" + (end - start));
}
复制代码

}

转载于:https://juejin.im/post/5c1df664e51d454ad55f19e2

java生成缩略图类源码相关推荐

  1. java的String类源码详解

    java的String类源码详解 类的定义 public final class Stringimplements java.io.Serializable, Comparable<String ...

  2. java.lang.object源码_第三篇:java.lang.Object 类源码分析

    Object所包含的方法如下: ① public Object(); 构造函数: 大部分情况下,类对象的声明,都是通过构造函数完成的(Java中规定:在类定义过程中,对于未定义构造函数的类,默认会有一 ...

  3. Java FileReader InputStreamReader类源码解析

    FileReader 前面介绍FileInputStream的时候提到过,它是从文件读取字节,如果要从文件读取字符的话可以使用FileReader.FileReader是可以便利读取字符文件的类,构造 ...

  4. Java集合---Arrays类源码解析

    一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型: ...

  5. java生成三角网_源码:基于离散点的构TIN算法(三角网)

    [实例简介]含实验报告以及测试数据 基于离散点的构TIN算法 一.算法思想 (1)首先,找出离散点集中相距最近的两点,连接这两点形成TI.的初始基线. (2) 然后,找出包含此基线的另外一个点构成三角 ...

  6. java 生成缩略图类_JAVA生成【缩略图】方法

    /** * 创建缩略图片 * * @param orgpath * @param filename * @return * @description: 描述 */ //此方法对于ssh项目并且针对 上 ...

  7. android sdk源码 andoid-21 下的TextUtils.java文本工具类 源码赏析

    下面这个是android sdk自带的文本工具,比如提供EditText对象的内容是否为空判断,截取字符串啊等等 对外提供的方法都是以静态方法的方式提供 /* Copyright © 2006 The ...

  8. java.lang 源码剖析_java.lang.Void类源码解析

    在一次源码查看ThreadGroup的时候,看到一段代码,为以下: /* * @throws NullPointerException if the parent argument is {@code ...

  9. Thread类源码剖析

    目录 1.引子 2.JVM线程状态 3.Thread常用方法 4.拓展点 一.引子 说来也有些汗颜,搞了几年java,忽然发现竟然没拜读过java.lang.Thread类源码,这次特地拿出来晒一晒. ...

最新文章

  1. 使用游戏测试干式EEG传感器的有效性
  2. math.hypot java_Java math
  3. linux+dhcp服务的安装包,服务器_Linux教程:配置DHCP服务器方法介绍,  1.安装dhcp软件包 #rpm nd - phpStudy...
  4. Fallout 3完结
  5. spoj1026 favorite dice
  6. laravel mysql 配置,laravel5数据库配置及其注意事项
  7. [react] 可以使用TypeScript写React应用吗?怎么操作?
  8. linux头文件 库,Linux操作系统的头文件和库文件搜索路径
  9. Apache java文件比对,Java Apache Commons的字符串比较
  10. [转载] 使用DirectInput进行交互
  11. eureka 客户端服务启动了又失败了_Spring cloud Eureka服务注册与发现详解
  12. xp sp3不让dword shoot
  13. 【黑马Java笔记+踩坑汇总】JavaSE+JavaWeb+SSM+SpringBoot+瑞吉外卖+SpringCloud/SpringCloudAlibaba+黑马旅游+谷粒商城
  14. Windows 提示“缺少所需的 CD/DVD 驱动器设备驱动程序”
  15. 这不是一篇技术型的文章,而是一篇能让你在IT世界中畅游的方法
  16. Android USB Tethering的实现以及代码流程
  17. 专精特新小巨人企业是什么
  18. 软考中级软件设计师--下午题
  19. Linux查看文件内容的方法
  20. 随机信号处理的一些归纳

热门文章

  1. 动力节点Java培训告诉你Java线程的多功能用法
  2. 必须要懂得的密码技术
  3. Android4.2以后,多屏幕的支持 学习(一)
  4. ANT无线通信技术(2) 通道配置
  5. Linux ALSA声卡驱动之八:ASoC架构中的Platform
  6. 远程手机测试机房的建立
  7. hi3516a的文件系统错误
  8. eselasticsearch入门_ElasticSearch入门学习-基础示例(1)
  9. dubbo接口测试_Django测试工具平台之Dubbo接口请求 + 前端
  10. 哪个牌子的平板电脑好_中山密码锁哪个牌子好