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

本文介绍如何使用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.imageio.plugins.png.PNGMetadata读取图片的元数据的更多相关文章

  1. Android中读取图片EXIF元数据之metadata-extractor的使用

    一.引言及介绍 近期在开发中用到了metadata-extractor-xxx.jar 和 xmpcore-xxx.jar这个玩意, 索性查阅大量文章了解学习,来分享分享. 本身工作也是常常和处理大图 ...

  2. Need ffmpeg exe. You can download it by calling: imageio.plugins.ffmpeg.download()

    该问题 Need ffmpeg exe. You can download it by calling: imageio.plugins.ffmpeg.download()往往出现在在调用 impor ...

  3. Servlet从本地文件中读取图片,并显示在页面中

    import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...

  4. java 读取图片色深

    问题: 想写一个小程序可读取图片的色深(bit-depth).网上有一些软件可完成这个功能,但是我想把程序做成一个可移植的插件. 本想用c写的,但实在麻烦,最后选择java,与很多方法不用自己写,速度 ...

  5. nodejs进阶(4)—读取图片到页面

    我们先实现从指定路径读取图片然后输出到页面的功能. 先准备一张图片imgs/dog.jpg. file.js里面继续添加readImg方法,在这里注意读写的时候都需要声明'binary'.(file. ...

  6. HTML中上传与读取图片或文件(input file)----在路上(25)

    input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...

  7. 运用Mono.Cecil 反射读取.NET程序集元数据

    CLR自带的反射机智和API可以很轻松的读取.NET程序集信息,但是不能对程序集进行修改.CLR提供的是只读的API,但是开源项目Mono.Cecil不仅仅可以读取.NET程序集的元数据,还可以进行修 ...

  8. nodeJS基础08:读取图片

    1.读取图片 //server.js var http = require("http"); var readImage = require("./readImage&q ...

  9. opencv用imread( argv[1], 1)读取图片

    显示一幅图:主要是运用功能:imread namedWindow imshowimread:从字面意思我们就可以看懂,用来读取图片的:namedWindow:显然,我们也可以看到这是用来命名窗口名称的 ...

随机推荐

  1. centos6.5安装gtk开发环境

    0.说明 由于 centos 默认的桌面是 gnome 的,我们知道gnome 的依赖库就是 gtk,而 gtk 各个版本的差异性还是很大的,所以我们需要根据 centos 的不同版本选择和 cent ...

  2. android studio 改包名

    使用Android studio有一段时间了,但是每次修改包名的时候都是用一种简单粗暴的方式,那就是新建一个想要的包名,然后直接拖拽. 但是这样有个不好的地方就是每次都要去修改manifest.xml ...

  3. JS 检查是否在微信浏览器

    /** * 检查是否在微信浏览器 * zengkai */ function isWeixinBrowser(){ var ua = navigator.userAgent.toLowerCase() ...

  4. 升级了git版本后git clone报ssl错误的解决方法

    由于升级了git版本,git clone 的时候报了如下的错误 fatal: unable to access 'https://github.com/open-falcon/falcon-plus. ...

  5. SPA单页应用前后分离微信授权

    项目基于微信公众号开发,业务完全依赖微信授权,也就是用户进入页面已经完成授权获取到用户的OpenId. 需要有一个授权中间页:author.vue 基本实现思路: 无论使用哪个url进入页面都会先触发 ...

  6. glassfish cluster 搭建

    http://blog.csdn.net/wych1981/article/details/8815150

  7. git clone 指定新建本地库位置

  8. js 去除字符串空白符

    var a="      123456" varb=a.replace(/(^\s*)/g, "");

  9. linux 02 基础命令

    linux 02 基础命令 1.alias 别名 pyvip@Vip:~/demo$ alias lh="ls -lh" #将ls -lh的功能赋给lh(lh原来并没有意义)这个赋 ...

  10. 基于TCP协议网络编程

    1.TCP/IP是一种可靠的网络协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路: 一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路来进行通信: 2.Java对基 ...