java 获取图片信息:宽高、动图帧数、获取图片argb颜色值并转化为16进制等
/**
* 图片大小
* @param file
*/
public static void getSize(File file){
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
int width = bi.getWidth();
int height = bi.getHeight();
System.out.println("width=" + width + ",height=" + height + ".");
} catch (Exception e) {
e.printStackTrace();
}finally {
assert bi != null;
bi.flush();
}
}
动图总帧数
/**
* 总帧数
* @param file
*/
public static int getFrames(String absPath){
File file = new File(absPath);
FileImageInputStream in = null;
try {
in = new FileImageInputStream(file);
ImageReaderSpi readerSpi = new GIFImageReaderSpi();
GIFImageReader gifReader = (GIFImageReader) readerSpi.createReaderInstance();
gifReader.setInput(in);
int num = gifReader.getNumImages(true);
System.out.println("总帧数="+num );
return num;
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return -1;
}
静态图片像素点argb
/**
* 获取图片RGB数组
* @param filePath
* @return
*/
public static int[][] getImageGRB(String filePath) {
File file = new File(filePath);
int[][] result = null;
if (!file.exists()) {
return result;
}
try {
BufferedImage bufImg = ImageIO.read(file);
int height = bufImg.getHeight();
int width = bufImg.getWidth();
result = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//获取的是argb
result[i][j] = bufImg.getRGB(j, i);
//获取的是rgb
// result[i][j] = bufImg.getRGB(j, i & 0xFFFFFF);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
静态图片像素点argb转16进制
int[][] imageGRB = getImageGRB(str);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{"+"\r\n\t");
for (int [] in:imageGRB) {
for (int i :in) {
if (i==0){ stringBuilder.append("0x00").append(",").append("0x00").append(",").append("0x00").append(",").append("0x00").append(",");
} else {
String argb = Integer.toHexString(i);
if (argb.length()<8){
int ap=8-argb.length();
for (int j = 0; j <ap ; j++) {
argb="0"+argb;
}
}
String a = "0x"+argb.substring(0, 2);
String r = "0x"+argb.substring(2, 4);
String g = "0x"+argb.substring(4, 6);
String b = "0x"+argb.substring(6, 8);
stringBuilder.append(r).append(",").append(g).append(",").append(b).append(",").append(a).append(",");
}
}
stringBuilder.append("\r\n\t");
}
//去除最后一个逗号
String substring = stringBuilder.substring(0, stringBuilder.length() - 4);
substring=substring+"\r\n }";
System.out.println(substring);
动图获取像素点argb并转16进制
/**
* 动图解析
* @param file
* @param buffer
*/
public static void dynamicGraphAnalysis( java.io.File file,StringBuilder buffer){
FileImageInputStream in = null;
try {
in = new FileImageInputStream(file);
ImageReaderSpi readerSpi = new GIFImageReaderSpi();
GIFImageReader gifReader = (GIFImageReader) readerSpi.createReaderInstance();
gifReader.setInput(in);
int num = gifReader.getNumImages(true);
if (num==0){
return;
}
for (int i = 0; i < num; i++) {
BufferedImage bufImg = gifReader.read(i);
int height = bufImg.getHeight();
int width = bufImg.getWidth();
for (int j = 0; j <height ; j++) {
for (int k = 0; k <width ; k++) {
int argb = bufImg.getRGB(k, j);
if (argb==0){
buffer.append("0x00").append(", ").append("0x00").append(", ").append("0x00").append(", ").append("0x00").append(", ");
} else {
String argbString = Integer.toHexString(argb);
if (argbString.length()<8){
int ap=8-argbString.length();
for (int h = 0; h <ap ; h++) {
argbString="0"+argbString;
}
}
String a = "0x"+argbString.substring(0, 2);
String r = "0x"+argbString.substring(2, 4);
String g = "0x"+argbString.substring(4, 6);
String b = "0x"+argbString.substring(6, 8);
buffer.append(r).append(", ").append(g).append(", ").append(b).append(", ").append(a).append(", ");
}
}
buffer.append("\r\n\t");
}
// buffer.append("\r\n\t");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}