所謂圖片元數(shù)據(jù),就是除了我們?nèi)庋劭吹降膱D片內(nèi)容外,隱藏在這些內(nèi)容背后的一些技術(shù)數(shù)據(jù)。
創(chuàng)新互聯(lián)自2013年起,先為玉田等服務(wù)建站,玉田等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為玉田企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
本文介紹如何使用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的圖片文件的內(nèi)容讀到字節(jié)數(shù)組content中。
getContent方法的代碼:
一張png圖片的元數(shù)據(jù),散布在下面這些節(jié)點里:
printNode(pngmeta.getStandardChromaNode()); printNode(pngmeta.getStandardCompressionNode()); printNode(pngmeta.getStandardDataNode()); printNode(pngmeta.getStandardDimensionNode()); printNode(pngmeta.getStandardDocumentNode()); printNode(pngmeta.getStandardTextNode()); printNode(pngmeta.getStandardTransparencyNode());
通過printNode打印出來:
printNode方法的源代碼:
打印出來的元數(shù)據(jù):
如果大家想要復制粘貼,這是全部的源代碼:
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的原創(chuàng)文章,請關(guān)注公眾號"汪子熙":
當前名稱:使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的元數(shù)據(jù)
網(wǎng)頁地址:http://jinyejixie.com/article40/gpspeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、小程序開發(fā)、品牌網(wǎng)站設(shè)計、響應(yīng)式網(wǎng)站、定制開發(fā)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)