Java里判断Image文件信息格式

1,判断Image格式
用UE打开GIF/PNG/JPG格式的图片文件
我们会发现在文件头部某几个位置的字节的值连起来是'GIF'/'PNG'/'JFIF'
它们的位置分别如下:
GIF: 012
JFIF(JPG): 6789
PNG: 123

这样我们可以通过判断这几个字节值来得到Image文件格式:

    String type = "";  
    byte b0 = image.getFileData()[0];  
    byte b1 = image.getFileData()[1];  
    byte b2 = image.getFileData()[2];  
    byte b3 = image.getFileData()[3];  
    byte b6 = image.getFileData()[6];  
    byte b7 = image.getFileData()[7];  
    byte b8 = image.getFileData()[8];  
    byte b9 = image.getFileData()[9];  
    // GIF  
    if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F')  
      type = "GIF";  
    // PNG  
    else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G')  
      type = "PNG";  
    // JPG  
      else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I' && b9 == (byte) 'F')  
      type = "JPG";  
    else  
      type = "Unknown";  
      image.setType(type); 


2,判断Image大小

FileImageInputStream fiis = new FileImageInputStream(new File(image.getPath())); 

image.setSize((float) fii.length() / 1000 + "KB"); 


3,判断Image宽度和高度

ImageIcon ii = new ImageIcon(image.getPath()); 

image.setHeight(String.valueOf(ii.getIconHeight())); 

image.setWidth(String.valueOf(ii.getIconWidth()));



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过文件的后缀名来判断文件类型,也可以通过文件的魔数来判断文件类型。 1. 通过后缀名判断文件类型: ```java import java.io.File; public class FileType { public static void main(String[] args) { File file = new File("test.txt"); String fileName = file.getName(); String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); if ("txt".equalsIgnoreCase(suffix)) { System.out.println("text file"); } else if ("jpg".equalsIgnoreCase(suffix) || "jpeg".equalsIgnoreCase(suffix)) { System.out.println("image file"); } else if ("doc".equalsIgnoreCase(suffix) || "docx".equalsIgnoreCase(suffix)) { System.out.println("word file"); } else { System.out.println("unknown file type"); } } } ``` 2. 通过文件的魔数判断文件类型: 文件的魔数是指文件的一些特定字节,不同文件类型的魔数是不同的。通过读取文件的前几个字节来判断文件类型。 ```java import java.io.FileInputStream; import java.io.IOException; public class FileType { public static void main(String[] args) throws IOException { FileInputStream inputStream = new FileInputStream("test.txt"); byte[] bytes = new byte[4]; inputStream.read(bytes, 0, 4); String fileHead = bytesToHexString(bytes); if (fileHead.startsWith("FFD8FF")) { System.out.println("image file"); } else if (fileHead.startsWith("89504E47")) { System.out.println("image file"); } else if (fileHead.startsWith("47494638")) { System.out.println("image file"); } else if (fileHead.startsWith("49492A00")) { System.out.println("image file"); } else if (fileHead.startsWith("424D")) { System.out.println("image file"); } else if (fileHead.startsWith("41433130")) { System.out.println("audio file"); } else if (fileHead.startsWith("494433")) { System.out.println("audio file"); } else if (fileHead.startsWith("52494646")) { System.out.println("audio file"); } else { System.out.println("unknown file type"); } inputStream.close(); } // 将字节数组转换为16进制字符串 private static String bytesToHexString(byte[] bytes) { StringBuilder stringBuilder = new StringBuilder(); if (bytes == null || bytes.length <= 0) { return null; } for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值