所谓图片元数据,就是除了我们肉眼看到的图片内容外,隐藏在这些内容背后的一些技术数据。
本文介绍如何使用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方法的代码:
一张png图片的元数据,散布在下面这些节点里:
printNode(pngmeta.getStandardChromaNode());
printNode(pngmeta.getStandardCompressionNode());
printNode(pngmeta.getStandardDataNode());
printNode(pngmeta.getStandardDimensionNode());
printNode(pngmeta.getStandardDocumentNode());
printNode(pngmeta.getStandardTextNode());
printNode(pngmeta.getStandardTransparencyNode());
通过printNode打印出来:
printNode方法的源代码:
打印出来的元数据:
如果大家想要复制粘贴,这是全部的源代码:
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的原创文章,请关注公众号"汪子熙":
相关推荐
com.sun.media.imageio.plugins.tiff.TIFFImageWritePar 报错,很多同学找不到 这个jai_imageio-1.1-alpha.jar 提供下下载
做OCR项目时用到的tesseract相关技术工作包,找起来还蛮麻烦的,放在这里给各位技术朋友们免费下载。
主页:http://java.sun.com/javase/6/docs/technotes/guides/imageio/index.html JAI 中的 Image I/O Tools,支持更多图片类型,例如JPEG-LS, JPEG2000, 和 TIFF。 主页:https://jai-imageio.dev.java.net/。JAI ...
赠送jar包:jai-imageio-core-1.4.0.jar; 赠送原API文档:jai-imageio-core-1.4.0-javadoc.jar; 赠送源代码:jai-imageio-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:jai-imageio-core-1.4.0.pom; 包含...
javax.imageio-1.0.5.jar, java处理图片的库。 从maven中央仓库下载的。实测可用。
赠送jar包:jai-imageio-core-1.4.0.jar; 赠送原API文档:jai-imageio-core-1.4.0-javadoc.jar; 赠送源代码:jai-imageio-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:jai-imageio-core-1.4.0.pom; 包含...
一个可以支持通过Java进行图片转换的jar包,封装好了跨平台动态链接库调用支持。https://link.zhihu.com/?target=https%3A//github.com/nintha/webp-imageio-core
NULL 博文链接:https://o-oand0-0.iteye.com/blog/1585854
java高级图片处理工具 jai-imageio,可以修改图片分辨率等
赠送jar包:imageio-psd-3.1.1.jar; 赠送原API文档:imageio-psd-3.1.1-javadoc.jar; 赠送源代码:imageio-psd-3.1.1-sources.jar; 赠送Maven依赖信息文件:imageio-psd-3.1.1.pom; 包含翻译后的API文档:...
Tesseract中文语言包(chi_sim.traineddata)+opencv3.4.3winows版本安装包+swingx-1.6.1.jar+jai_imageio-1.1.jar
赠送jar包:imageio-core-3.1.1.jar; 赠送原API文档:imageio-core-3.1.1-javadoc.jar; 赠送源代码:imageio-core-3.1.1-sources.jar; 赠送Maven依赖信息文件:imageio-core-3.1.1.pom; 包含翻译后的API文档:...
java一般使用两个path:classpath 和 java.library.path classpath是指向jar包的位置 java.library.path是非java类包的位置如(dll,so) 解决办法: 1:LINUX下的系统变量LD_LIBRARY_PATH来添加java.library....
使用Jcrop.js和jQuery.form.js,用ImageIO等进行头像上传缩放及裁剪 http://blog.csdn.net/recordme/article/details/42550703
jai-imageio-core-1.3.0.1.jar jai-imageio-core-1.3.0.1.jar
用法将 webp-imageio.jar 添加到应用程序的类路径确保 libwebp-imageio.so、libwebp-imageio.dylib 或 webp-imageio.dll 可在 Java 本机库路径(java.library.path 系统属性)上访问WebP 读取器和写入器可以像任何...
赠送jar包:imageio-core-3.1.1.jar; 赠送原API文档:imageio-core-3.1.1-javadoc.jar; 赠送源代码:imageio-core-3.1.1-sources.jar; 赠送Maven依赖信息文件:imageio-core-3.1.1.pom; 包含翻译后的API文档:...
webp-imageio-0.4.2.zip 包含:.jar .so/.dll 涵盖linux 32/64 windows 32/64 版本
赠送jar包:imageio-tiff-3.1.1.jar; 赠送原API文档:imageio-tiff-3.1.1-javadoc.jar; 赠送源代码:imageio-tiff-3.1.1-sources.jar; 赠送Maven依赖信息文件:imageio-tiff-3.1.1.pom; 包含翻译后的API文档:...
赠送jar包:imageio-bmp-3.1.1.jar; 赠送原API文档:imageio-bmp-3.1.1-javadoc.jar; 赠送源代码:imageio-bmp-3.1.1-sources.jar; 赠送Maven依赖信息文件:imageio-bmp-3.1.1.pom; 包含翻译后的API文档:...