博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用com.sun.imageio.plugins.png.PNGMetadata读取图片的元数据
阅读量:2436 次
发布时间:2019-05-10

本文共 3715 字,大约阅读时间需要 12 分钟。

所谓图片元数据,就是除了我们肉眼看到的图片内容外,隐藏在这些内容背后的一些技术数据。

本文介绍如何使用Java代码将一张图片的隐藏信息读取出来。

首先不需要下载任何额外的Java库,用JDK自带的库就能工作。

import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import javax.imageio.ImageIO;import javax.imageio.ImageReader;import javax.imageio.metadata.IIOMetadata;import javax.imageio.metadata.IIOMetadataNode;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import com.sun.imageio.plugins.png.PNGMetadata;新建一个Java类,这个类的main方法也是非常直接的:static public void main(String[] arg) throws IOException{byte[] content = getContent("C:\Users\i042416\Desktop\test\clipboard1.png");readCustomData(content);}

首先把桌面上名叫clipboard1.png的图片文件的内容读到字节数组content中。

getContent方法的代码:

webp

一张png图片的元数据,散布在下面这些节点里:

printNode(pngmeta.getStandardChromaNode());printNode(pngmeta.getStandardCompressionNode());printNode(pngmeta.getStandardDataNode());printNode(pngmeta.getStandardDimensionNode());printNode(pngmeta.getStandardDocumentNode());printNode(pngmeta.getStandardTextNode());printNode(pngmeta.getStandardTransparencyNode());

通过printNode打印出来:

webp

printNode方法的源代码:

webp

打印出来的元数据:

webp

如果大家想要复制粘贴,这是全部的源代码:

package image;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import javax.imageio.ImageIO;import javax.imageio.ImageReader;import javax.imageio.metadata.IIOMetadata;import javax.imageio.metadata.IIOMetadataNode;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import com.sun.imageio.plugins.png.PNGMetadata;public class pngTest {static private byte[] getContent(String filePath) throws IOException {File file = new File(filePath);long fileSize = file.length();if (fileSize > Integer.MAX_VALUE) {System.out.println("file too big...");return null;}FileInputStream fi = new FileInputStream(file);byte[] buffer = new byte[(int) fileSize];int offset = 0;int numRead = 0;while (offset < buffer.length&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {offset += numRead;}if (offset != buffer.length) {fi.close();throw new IOException("Could not completely read file "+ file.getName());}fi.close();return buffer;}static private void readCustomData(byte[] imageData) throws IOException{ImageReader imageReader = ImageIO.getImageReadersByFormatName("png").next();imageReader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(imageData)), true);IIOMetadata metadata = imageReader.getImageMetadata(0);PNGMetadata pngmeta = (PNGMetadata) metadata;printNode(pngmeta.getStandardChromaNode());printNode(pngmeta.getStandardCompressionNode());printNode(pngmeta.getStandardDataNode());printNode(pngmeta.getStandardDimensionNode());printNode(pngmeta.getStandardDocumentNode());printNode(pngmeta.getStandardTextNode());printNode(pngmeta.getStandardTransparencyNode());}static private void printNode(IIOMetadataNode metanode){if (metanode == null)return;NodeList childNodes = metanode.getChildNodes();if( childNodes == null)return;for (int i = 0; i < childNodes.getLength(); i++) {Node node = childNodes.item(i);NamedNodeMap attribute = node.getAttributes();if( attribute == null)continue;int length = attribute.getLength();for( int j = 0; j < length; j++){Node each = attribute.item(j);String value = each.getNodeValue();String name = each.getNodeName();System.out.println("Name: " + name + " value: " + value);}}}static public void main(String[] arg) throws IOException{byte[] content = getContent("C:\Users\i042416\Desktop\test\clipboard1.png");readCustomData(content);}}

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

webp

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24475491/viewspace-2216519/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24475491/viewspace-2216519/

你可能感兴趣的文章
LINUX的系统内核空间的保护(转)
查看>>
在Visual C++中利用UDL文件建ADO连接(转)
查看>>
C++编程批评系列 继承的本质(转)
查看>>
unix下编写socket程序的一般步骤(转)
查看>>
共享软件中注册部分的简单实现(转)
查看>>
RedHat Linux 9下所有权和许可权限(转)
查看>>
C++程序设计从零开始之语句(转)
查看>>
利用Apache+PHP3+MySQL建立数据库驱动的动态网站(转)
查看>>
C#中实现DataGrid双向排序(转)
查看>>
利用C语言小程序来解决大问题(转)
查看>>
简单方法在C#中取得汉字的拼音的首字母(转)
查看>>
.NET开发之中的17种正则表达式小结(转)
查看>>
编程秘籍:使C语言高效的四大绝招(转)
查看>>
配置XDM--一种Linux的图形登录界面(转)
查看>>
计算机加锁 把U盘变成打开电脑的钥匙(转)
查看>>
C#开发的两个基本编程原则的深入讨论(转)
查看>>
Fedora Core 4 基础教程 (上传完毕)(转)
查看>>
删除MSSQL危险存储过程的代码(转)
查看>>
Mysql多台数据库同步问题(转)
查看>>
红旗软件:树立国际的Linux品牌(转)
查看>>