这个工具类完成的工作如下:

1、第一个static方法,完成图片格式的转换。统一转换成.jpg格式。

package util;

import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.DirectColorModel;
import java.awt.image.PixelGrabber;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
/*
* 处理图片的工具类:
* 定义了3个方法
*/
public class ImageUtil {
//图片格式的转换,统一转换成.jpg格式
public static BufferedImage change2jpg(File f) {
try {
java.awt.Image i = Toolkit.getDefaultToolkit().createImage(f.getAbsolutePath());
PixelGrabber pg = new PixelGrabber(i, 0, 0, -1, -1, true);
pg.grabPixels();
int width = pg.getWidth(), height = pg.getHeight();
final int[] RGB_MASKS = { 0xFF0000, 0xFF00, 0xFF };
final ColorModel RGB_OPAQUE = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]);
DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
BufferedImage img = new BufferedImage(RGB_OPAQUE, raster, false, null);
return img;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}

最新文章

  1. jQuery下操作dropdownlist
  2. adb permission denied
  3. codeForce-19D Points (点更新+离散化)
  4. SSH学习笔记目录
  5. C# 比较方法
  6. CoreLocation MKMapView 地图
  7. [Tutorial] Using the RasPi as a WiFi hostspot
  8. hardware_hp刀片服务器安装windows 2008系统配置
  9. grunt对象之api
  10. php中urldecode()和urlencode()起什么作用
  11. div在另一个div居中对齐
  12. centOS改编码
  13. Centos7 多网卡抓包可以抓到UDP但程序recvfrom不到
  14. 给大厨写的R数据分析代码
  15. 使用iTextSharp导出PDF
  16. 子序列的按位或 Bitwise ORs of Subarrays
  17. 第一章连通性问题-----algorithm in C 读书笔记
  18. Zookeeper使用场景
  19. AOP的最佳注入方式——MSIL注入
  20. Nginx视频流模块nginx-rtmp-module

热门文章

  1. qcow2快照原理
  2. 长江存储推全新3D NAND架构 挑战三星存储
  3. 数组输出黑科技----fwrite()
  4. python selenium(用例断言)
  5. Ajax各参数介绍及使用
  6. C. Helga Hufflepuff's Cup 树形dp 难
  7. IDEA破解,自动激活【2020年版本也可以破解】
  8. node能做的性能优化
  9. 08_CSS入门和高级技巧(6)
  10. HttpClient之Post接口代码范例