java获取webp格式图片宽和高 以及普通文件的宽和高 图片宽高
// 主图地址_wh
String imgFileName1 = "";
String[] fileNames;
if (facePathFile != null) {
fileNames = uploadFile(facePathFile, facePathFileFileName, "img");
imgFileName1 = fileNames[0];
bean.setFacePath(imgFileName1);
try {
// ImageIO.read报错的解决方法
String fileType = ImgeMimeTypeUtils.getMimeType(facePathFile.getAbsolutePath());
if(fileType != null && "image/webp".equals(fileType)){
FileInputStream file = new FileInputStream(facePathFile);
byte[] bytes = new byte[30];
file.read(bytes,0,bytes.length);
int width = ((int) bytes[27] & 0xff)<<8 | ((int) bytes[26] & 0xff);
int height = ((int) bytes[29] & 0xff)<<8 | ((int) bytes[28] & 0xff);
bean.setThumbWidth(width);
bean.setThumbHeight(height);
}else{
// 普通图片 获取宽高
BufferedImage sourceImg;
sourceImg = ImageIO.read(facePathFile);
bean.setThumbWidth(sourceImg.getWidth());
bean.setThumbHeight(sourceImg.getHeight());
}
} catch (Exception e) {
try {
ThumbnailConvert tc = new ThumbnailConvert();
tc.setCMYK_COMMAND(facePathFile.getAbsolutePath());
Image image = null;
image = Toolkit.getDefaultToolkit().getImage(
facePathFile.getAbsolutePath());
MediaTracker mediaTracker = new MediaTracker(
new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
bean.setThumbWidth(image.getWidth(null));
bean.setThumbHeight(image.getHeight(null));
} catch (Exception e1) {
bean.setThumbWidth(0);
bean.setThumbHeight(0);
}
}
package com.ebillion.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ImgeMimeTypeUtils {
public static void main(String[] args) {
String fileHead="E:/user_notes_mp4/meishi_video/100073853205/mp4_to_webp/";
String copyFileHead = fileHead + "aaatimg.gif";
File file1 = new File(copyFileHead);
File file2 = new File(
"F:/log_aliyun_B-2/web_8559UjhSZR3wWTjqMsA_3_c.jpg");
System.out.println(System.getProperty("java.library.path"));
//System.load("/mnt/www/wwwroot/auto_caiji/lib/webp/libwebp-imageio.so");
System.out.println(getMimeType(file1.getAbsolutePath()));
/*try {
if (file1.exists()) {
BufferedImage im = ImageIO.read(file1);
ImageIO.write(im, "jpg", file2);
} else {
System.out.println("--->file1.exists()--->" + file1.exists());
}
} catch (IOException e) {
e.printStackTrace();
}*/
System.out.println("--->image convert success--->");
}
/**
* 获取文件的mimeType
* @param filename
* @return
*/
public static String getMimeType(String filename) {
try {
String mimeType = readType(filename);
return String.format("image/%s", mimeType);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 读取文件类型
* @param filename
* @return
* @throws IOException
*/
public static String readType(String filename) throws IOException {
FileInputStream fis = null;
try {
File f = new File(filename);
if (!f.exists() || f.isDirectory() || f.length() < 8) {
throw new IOException("the file [" + f.getAbsolutePath()
+ "] is not image !");
}
fis = new FileInputStream(f);
byte[] bufHeaders = readInputStreamAt(fis, 0, 8);
if (isJPEGHeader(bufHeaders)) {
long skiplength = f.length() - 2 - 8; //第一次读取时已经读了8个byte,因此需要减掉
byte[] bufFooters = readInputStreamAt(fis, skiplength, 2);
if (isJPEGFooter(bufFooters)) {
return "jpeg";
}
}
if (isPNG(bufHeaders)) {
return "png";
}
if (isGIF(bufHeaders)) {
return "gif";
}
if (isWEBP(bufHeaders)) {
return "webp";
}
if (isBMP(bufHeaders)) {
return "bmp";
}
if (isICON(bufHeaders)) {
return "ico";
}
throw new IOException("the image's format is unkown!");
} catch (FileNotFoundException e) {
throw e;
} finally {
try {
if (fis != null)
fis.close();
} catch (Exception e) {
}
}
}
/**
* 标示一致性比较
* @param buf 待检测标示
* @param markBuf 标识符字节数组
* @return 返回false标示标示不匹配
*/
private static boolean compare(byte[] buf, byte[] markBuf) {
for (int i = 0; i < markBuf.length; i++) {
byte b = markBuf[i];
byte a = buf[i];
if (a != b) {
return false;
}
}
return true;
}
/**
*
* @param fis 输入流对象
* @param skiplength 跳过位置长度
* @param length 要读取的长度
* @return 字节数组
* @throws IOException
*/
private static byte[] readInputStreamAt(FileInputStream fis,
long skiplength, int length) throws IOException {
byte[] buf = new byte[length];
fis.skip(skiplength); //
int read = fis.read(buf, 0, length);
return buf;
}
private static boolean isBMP(byte[] buf){
byte[] markBuf = "BM".getBytes(); //BMP图片文件的前两个字节
return compare(buf, markBuf);
}
private static boolean isICON(byte[] buf) {
byte[] markBuf = {0, 0, 1, 0, 1, 0, 32, 32};
return compare(buf, markBuf);
}
private static boolean isWEBP(byte[] buf) {
byte[] markBuf = "RIFF".getBytes(); //WebP图片识别符
return compare(buf, markBuf);
}
private static boolean isGIF(byte[] buf) {
byte[] markBuf = "GIF89a".getBytes(); //GIF识别符
if(compare(buf, markBuf))
{
return true;
}
markBuf = "GIF87a".getBytes(); //GIF识别符
if(compare(buf, markBuf))
{
return true;
}
return false;
}
private static boolean isPNG(byte[] buf) {
byte[] markBuf = {(byte) 0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A}; //PNG识别符
// new String(buf).indexOf("PNG")>0 //也可以使用这种方式
return compare(buf, markBuf);
}
private static boolean isJPEGHeader(byte[] buf) {
byte[] markBuf = {(byte) 0xff, (byte) 0xd8}; //JPEG开始符
return compare(buf, markBuf);
}
private static boolean isJPEGFooter(byte[] buf)//JPEG结束符
{
byte[] markBuf = {(byte) 0xff, (byte) 0xd9};
return compare(buf, markBuf);
}
}
上一篇: VBS教程:函数-DateDiff 函数
下一篇: 营销型网站建设和响应式网站建设如何选择?