二、工具类ImageUtil——图片处理
2024-10-09 05:24:19
这个工具类完成的工作如下:
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;
}
}
}
最新文章
- jQuery下操作dropdownlist
- adb permission denied
- codeForce-19D Points (点更新+离散化)
- SSH学习笔记目录
- C# 比较方法
- CoreLocation MKMapView 地图
- [Tutorial] Using the RasPi as a WiFi hostspot
- hardware_hp刀片服务器安装windows 2008系统配置
- grunt对象之api
- php中urldecode()和urlencode()起什么作用
- div在另一个div居中对齐
- centOS改编码
- Centos7 多网卡抓包可以抓到UDP但程序recvfrom不到
- 给大厨写的R数据分析代码
- 使用iTextSharp导出PDF
- 子序列的按位或 Bitwise ORs of Subarrays
- 第一章连通性问题-----algorithm in C 读书笔记
- Zookeeper使用场景
- AOP的最佳注入方式——MSIL注入
- Nginx视频流模块nginx-rtmp-module