ImageUtil.java
2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.arcsoft.facedemo.util;
import com.arcsoft.facedemo.base.ImageInfo;
import javax.imageio.ImageIO;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class ImageUtil {
public static ImageInfo getRGBData(File file) {
if (file == null)
return null;
ImageInfo imageInfo;
try {
//将图片文件加载到内存缓冲区
BufferedImage image = ImageIO.read(file);
imageInfo=bufferedImage2ImageInfo(image);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return imageInfo;
}
public static ImageInfo getRGBData(InputStream input) {
if (input == null)
return null;
ImageInfo imageInfo;
try {
BufferedImage image = ImageIO.read(input);
imageInfo=bufferedImage2ImageInfo(image);
} catch (IOException e) {
return null;
}finally {
if(input!=null){
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return imageInfo;
}
public static ImageInfo bufferedImage2ImageInfo(BufferedImage image){
ImageInfo imageInfo = new ImageInfo();
int width = image.getWidth();
int height = image.getHeight();
// 使图片居中
width = width & (~3);
height = height & (~3);
imageInfo.width = width;
imageInfo.height = height;
//根据原图片信息新建一个图片缓冲区
BufferedImage resultImage = new BufferedImage(width, height, image.getType());
//得到原图的rgb像素矩阵
int[] rgb = image.getRGB(0, 0, width, height, null, 0, width);
//将像素矩阵 绘制到新的图片缓冲区中
resultImage.setRGB(0, 0, width, height, rgb, 0, width);
//进行数据格式化为可用数据
BufferedImage dstImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
if(resultImage.getType() != BufferedImage.TYPE_3BYTE_BGR) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
ColorConvertOp colorConvertOp = new ColorConvertOp(cs, dstImage.createGraphics().getRenderingHints());
colorConvertOp.filter(resultImage, dstImage);
} else {
dstImage = resultImage;
}
//获取rgb数据
imageInfo.rgbData = ((DataBufferByte) (dstImage.getRaster().getDataBuffer())).getData();
return imageInfo;
}
}