使用图像播放Java中的一种技巧-搜索图像,将图像转换为文本,隐藏数据
在本文中,我將為您提供一種處理圖像文件的方法。 本文將使您深入了解Java中的一些技巧,以便您可以隱藏圖像內(nèi)的敏感信息,將完整圖像隱藏為文本,在目錄內(nèi)搜索特定圖像,并最小化圖像的大小。 但是,這不是一個新概念,有一個稱為隱寫術(shù)的概念,可以將您的秘密信息隱藏在圖像中。 對于此概念,可以使用許多非常復雜的軟件來處理圖像。 但作為Java開發(fā)人員,我們也可以
在一定程度上實現(xiàn)它。
通常,許多人將所有個人圖像保存在特定目錄中,其他人則通過搜索* .jpg來查看圖像。
它妨礙了您的照片和圖像的隱私。 在本文中,我將向您展示如何將圖片,照片和圖像轉(zhuǎn)換為文本文件以提高隱私性。 其他人可以查看您的文本文件,但看不懂它是什么。 有時,許多用戶將所有秘密和敏感信息(例如幾個銀行詳細信息,信用卡號,電子郵件密碼詳細信息)放在文本文件中,以便他們可以輕松登錄特定的應(yīng)用程序。 但是,當其他人使用這些信息所在的系統(tǒng)時,就會產(chǎn)生問題。 在本文中,我將向您展示如何在圖像中隱藏個人詳細信息,以及如何在需要時檢索詳細信息。 讓我們進入技術(shù)細節(jié),因為我不想延長我的描述。
技術(shù)性在Java中,讀取圖像非常容易,您可以將圖像內(nèi)容存儲為平面文件。 為此,請使用Java中的ImageIO類讀取圖像文件,并使用Base64Encoder將字節(jié)數(shù)組轉(zhuǎn)換為String。 現(xiàn)在您的主要工作已經(jīng)結(jié)束,現(xiàn)在您可以以任何方式操作String了。 讓我們調(diào)查一下案例。
案例– 1:將照片隱藏為文本文件在這種情況下,您可以將所有照片或圖像隱藏到文本文件中,并以其他用戶將其視為系統(tǒng)文件或日志的方式重命名文件。 在我的類“ ImageAnalyzerUtil”中,有一種將圖像轉(zhuǎn)換為文本文件的方法。 方法名稱為“ convertImageToText()”。 在這種方法中,使用“ Base64Encoder”將圖像文件讀取為字符串,最后將其寫入文本文件。
情況– 2:從文本文件中獲取原始圖像在“ ImageAnalyzerUtil”類中,有一個名為“ convertTextToImage()”的方法,該方法會將文本文件轉(zhuǎn)換為圖像。 但是,所有文本文件都不會轉(zhuǎn)換為圖像。 只有那些已從圖像文件轉(zhuǎn)換的文本文件才能創(chuàng)建圖像。 在這種情況下,將讀取轉(zhuǎn)換后的文本文件,并使用“ Base64Decoder”將其轉(zhuǎn)換為字節(jié)數(shù)組,最后將字節(jié)數(shù)組寫入文件中。
案例– 3:將敏感信息隱藏在圖像中在這種情況下,類“ ImageAnalyzerUtil”中有一個稱為“ hideTextDataInsideImage()”的方法。 此處,一個額外的字符串與其他數(shù)據(jù)一起添加到了圖像文件中,以便可以輕松檢索數(shù)據(jù)。
情況– 4:從圖像中檢索您的敏感信息為此,在類“ ImageAnalyzerUtil”中有一個稱為“ getHiddenDataFromImage()”的方法。 在這種情況下,整個圖像將被讀取為String,并且敏感信息將被分離并顯示。
情況– 5:最小化圖像尺寸在使用該程序時,我發(fā)現(xiàn)從文本文件還原原始圖像時,圖像的大小減小了。 不會丟失圖像數(shù)據(jù),但是可能會丟失OS提供的某些額外數(shù)據(jù)。
下面給出了完整的程序以及所有相關(guān)的Testharness程序。
package?com.ddsoft.tornado.core.image; import?java.awt.image.BufferedImage; import?java.io.BufferedOutputStream; import?java.io.BufferedReader; import?java.io.ByteArrayOutputStream; import?java.io.DataInputStream; import?java.io.File; import?java.io.FileInputStream; import?java.io.FileNotFoundException; import?java.io.FileOutputStream; import?java.io.IOException; import?java.io.InputStream; import?java.io.InputStreamReader; import?java.io.OutputStream; import?java.io.OutputStreamWriter;? import?javax.imageio.ImageIO;? /***?This?class?contains?the?following?utility?methods.*?<p>*?<li>?Utility?Method?to?read?converted?image?text?file?</li>*?<li>?Utility?method?to?write?the?image?as?text?file?</li>*?<li>?Utility?method?to?get?the?image?contents?as?String?</li>*?<li>?Utility?method?hide?the?text?data?inside?an?image?</li>*?<li>?Utility?method?get?the?hidden?data?from?an?image?</li>*?<li>?Utility?method?to?search?a?particular?image?inside?a?directory?</li>*?<li>?Utility?method?to?search?and?to?obtain?the?full?path?of?an?image?</li>*?<li>?Utility?method?to?convert?an?image?into?a?text?file?</li>*?<li>?Utility?method?to?convert?a?text?back?into?image?</li>*?@author?Debadatta?Mishra(PIKU)**/ public?class?ImageAnalyzerUtil? {/***?String?type?constant?to?define?the?image?type.*/private?static?String?IMAGE_TYPE?=?"jpg";/***?An?extra?String?that?separates?the?image?data?and?the?secret?information?data*/private?static?String?extraStr?=?"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";?/***?This?method?is?used?to?read?the?contents?of?a?text(?converted?from?image?)?file*?and?provides?byte[].*?@param?imageTextFilePath?of?type?String?indicating?the?path?of?the?text?file*?@return?an?array?of?bytes*?@author?Debadatta?Mishra(PIKU)*/public?static?byte[]?readImageTextFile(?String?imageTextFilePath?){byte[]?dataFile?=?null;try{StringBuffer?textData?=?new?StringBuffer();FileInputStream?fstream?=?new?FileInputStream(imageTextFilePath);DataInputStream?in?=?new?DataInputStream(fstream);BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(in));String?srtData;while?((srtData?=?br.readLine())?!=?null)?{textData.append(srtData);}br.close();fstream.close();in.close();dataFile?=?new?sun.misc.BASE64Decoder().decodeBuffer(textData.toString());}catch(?Exception?e?){e.printStackTrace();}return?dataFile;}?/***?This?method?is?used?to?write?the?contents?of?an?image?into?a?text?file.*?@param?filePath?of?type?String?indicating?the?path?of?the?text?file*?@param?imageContents?of?type?String?indicating?the?image?contents?as?String*?@author?Debadatta?Mishra(PIKU)*/public?static?void?writeImageAsTextFile(?String?filePath?,?String?imageContents?){FileOutputStream?fout=null;OutputStreamWriter?osw?=?null;OutputStream?bout?=?null;try{fout?=?new?FileOutputStream?(filePath);bout=?new?BufferedOutputStream(fout);osw?=?new?OutputStreamWriter(bout,?"utf-8");osw.write(imageContents);bout.close();osw.close();fout.close();}catch(?Exception?e?){e.printStackTrace();}}?/***?This?method?is?used?to?get?the?image?contents?as?String.*?@param?imagePath?of?type?String?indicating?the?path?of?image?file*?@return?a?String?of?image?contents*?@author?Debadatta?Mishra(PIKU)*/public?static?String?getImageAsString(?String?imagePath?){String?imageString?=?null;try{File?f?=?new?File(imagePath);BufferedImage?buffImage?=?ImageIO.read(f);ByteArrayOutputStream?os=?new?ByteArrayOutputStream();ImageIO.write(buffImage,?IMAGE_TYPE,?os);byte[]?data=?os.toByteArray();imageString?=?new?sun.misc.BASE64Encoder().encode(data);}catch(?FileNotFoundException?fnfe?){fnfe.printStackTrace();System.out.println("Image?is?not?located?in?the?proper?path.");}catch?(IOException?e)?{e.printStackTrace();System.out.println("Error?in?reading?the?image.");}return?imageString;}?/***?This?method?is?used?to?hide?the?data?contents?inside?the?image.*?@param?srcImagePath?of?type?String?indicating?the?path?of?the?source?image*?@param?dataContents?of?type?String?containing?data*?@author?Debadatta?Mishra(PIKU)*/public?static?void?hideTextDataInsideImage(?String?srcImagePath?,?String?dataContents?){try?{dataContents?=?new?sun.misc.BASE64Encoder().encode(dataContents.getBytes());extraStr?=?new?sun.misc.BASE64Encoder().encode(extraStr.getBytes());FileOutputStream?fos?=?new?FileOutputStream(?srcImagePath?,?true?);fos.write(new?sun.misc.BASE64Decoder().decodeBuffer(extraStr.toString()));fos.write(?dataContents.getBytes()?);fos.close();}catch(?FileNotFoundException?fnfe?){fnfe.printStackTrace();}catch?(IOException?e){e.printStackTrace();}catch(?Exception?e?){e.printStackTrace();}}?/***?This?method?is?used?to?get?the?hidden?data?from?an?image.*?@param?imagePath?of?type?String?indicating?the?path?of?the?image*?which?contains?the?hidden?data*?@return?the?String?containing?hidden?data?inside?an?image*?@author?Debadatta?Mishra(PIKU)*/public?static?String?getHiddenDataFromImage(?String?imagePath?){String?dataContents?=?null;try{File?file?=?new?File(?imagePath?);byte[]?fileData?=?new?byte[?(int)file.length()];InputStream?inStream?=?new?FileInputStream(?file?);inStream.read(fileData);inStream.close();String?tempFileData?=?new?String(fileData);String?finalData?=?tempFileData.substring(tempFileData.indexOf(extraStr)+?extraStr.length(),?tempFileData.length());byte[]?temp?=?new?sun.misc.BASE64Decoder().decodeBuffer(finalData);dataContents?=?new?String(temp);}catch(?Exception?e?){e.printStackTrace();}return?dataContents;}?/***?This?method?is?used?to?search?a?particular?image?in?a?image?directory.*?In?this?method,?it?will?search?for?the?image?contents?for?the?image*?you?are?passing.*?@param?imageToSearch?of?type?String?indicating?the?file?name?of?the?image*?@param?imageFolderToSearch?of?type?String?indicating?the?name?of?the?directory*?which?contains?the?images*?@return?true?if?image?is?found?else?false*?@author?Debadatta?Mishra(PIKU)*/public?static?boolean?searchImage(?String?imageToSearch?,?String?imageFolderToSearch?){boolean?searchFlag?=?false;try{String?searchPhotoStr?=?getImageAsString(imageToSearch);File?files?=?new?File(?imageFolderToSearch?);File[]?photosFiles?=?files.listFiles();for(?int?i?=?0?;?i?<?photosFiles.length?;?i++?){String?photoFilePath?=?photosFiles[i].getAbsolutePath();String?photosStr?=?getImageAsString(photoFilePath);if(?searchPhotoStr.equals(photosStr)){searchFlag?=?true;break;}else{continue;}}????}catch(?Exception?e?){e.printStackTrace();}return?searchFlag?;}?/***?This?method?is?used?to?search?for?a?particular?image?found?in?a?directory*?and?it?will?return?the?full?path?of?the?image?found.?Sometimes?it?is?required*?to?find?out?the?particular?image?and?the?path?of?the?image?so?that?the?path*?String?can?be?used?for?further?processing.*?@param?imageToSearch?of?type?String?indicating?the?file?name?of?the?image*?@param?imageFolderToSearch?of?type?String?indicating?the?name?of?the?directory*?which?contains?the?images*?@return?the?full?path?of?the?image*?@author?Debadatta?Mishra(PIKU)*/public?static?String?searchAndGetImageName(?final?String?imageToSearch?,?final?String?imageFolderToSearch?){String?foundImageName?=?null;try{String?searchPhotoStr?=?ImageAnalyzerUtil.getImageAsString(imageToSearch);File?files?=?new?File(?imageFolderToSearch?);File[]?photosFiles?=?files.listFiles();for(?int?i?=?0?,?n?=?photosFiles.length;?i?<?n?;?i++?){final?String?photoFilePath?=?photosFiles[i].getAbsolutePath();final?String?photosStr?=?ImageAnalyzerUtil.getImageAsString(photoFilePath);if(?searchPhotoStr.equals(photosStr)){foundImageName?=?photosFiles[i].getAbsolutePath();break;}else{continue;}}????}catch(?Exception?e?){e.printStackTrace();}return?foundImageName;}?/***?This?method?is?used?to?convert?an?image?into?a?text?file.*?@param?imagePath?of?type?String?indicating?the?path?of?the?image?file*?@author?Debadatta?Mishra(PIKU)*/public?static?void?convertImageToText(?String?imagePath?){File?file?=?new?File(?imagePath?);String?fileName?=?file.getAbsolutePath();String?textFilePath?=?new?StringBuilder(fileName.substring(0,?fileName.lastIndexOf("."))).append(".txt").toString();writeImageAsTextFile(textFilePath,?getImageAsString(imagePath));/**?There?may?be?requirement?to?delete?the?original?image,*?write?the?code?here?for?this?purpose.*/}?/***?This?method?is?used?to?convert?the?text?file?into?image.*?However?all?text?files?will?not?be?converted?into?images.*?Those?text?files?which?have?been?converted?from?images?files*?will?be?converted?back?into?image.*?@param?imageTextFilePath?of?type?String?indicating?the?converted?text?file*?from?image?file.*?@author?Debadatta?Mishra(PIKU)*/public?static?void?convertTextToImage(?String?imageTextFilePath?){try{File?file?=?new?File(?imageTextFilePath?);String?fileName?=?file.getAbsolutePath();String?imageFilePath?=?new?StringBuilder(fileName.substring(0,?fileName.lastIndexOf("."))).append(".").append(IMAGE_TYPE).toString();OutputStream?out?=?new?FileOutputStream(?imageFilePath?);byte[]?imageBytes?=?readImageTextFile(imageTextFilePath);out.write(imageBytes);out.close();}catch(?Exception?e?){e.printStackTrace();}}? }?以上是實現(xiàn)圖像功能的通用程序。 我在測試用例下方提供了支持上述程序的信息。以下測試程序提供了將轉(zhuǎn)換為文本文件(反之亦然)的詳細信息。
package?com.ddsoft.tornado.image.test;? import?com.ddsoft.tornado.core.image.ImageAnalyzerUtil;?? /***?This?is?a?test?harness?class?to?test?the*?image?conversion?utility.*?@author?Debadatta?Mishra(PIKU)**/ public?class?ConvertImageTest? {public?static?void?main(String[]?args)?{String?sourceImagePath?=?"data/IMG_2526.JPG";//Convert?the?image?into?text?fileImageAnalyzerUtil.convertImageToText(sourceImagePath);?//Convert?the?image?text?file?back?into?actual?image?fileImageAnalyzerUtil.convertTextToImage("data/IMG_2526.txt");}? }?下面的testharness程序演示了如何在圖像內(nèi)部隱藏數(shù)據(jù)。 package?com.ddsoft.tornado.image.test;? import?com.ddsoft.tornado.core.image.ImageAnalyzerUtil;? /***?This?is?a?testharness?class?to?hide?the?information?inside?an?image*?@author?Debadatta?Mishra(PIKU)**/ public?class?HideDataTest? {public?static?void?main(String[]?args){String?secretData?=?"It?contains?all?my?secret?materials?and?my?bank?information?details";String?destinationImathPath?=?"data/secretImage.jpg";//Hide?the?information?inside?the?imageImageAnalyzerUtil.hideTextDataInsideImage(destinationImathPath,secretData);//Get?back?the?data?hidden?inside?an?imageString?hiddenData?=?ImageAnalyzerUtil.getHiddenDataFromImage(destinationImathPath);System.out.println(hiddenData);}? }?以下測試程序顯示了如何在圖像目錄中搜索圖像。 package?com.ddsoft.tornado.image.test;? import?com.ddsoft.tornado.core.image.ImageAnalyzerUtil;? /***?This?is?a?testharness?class?to?search?for?an?image*?@author?Debadatta?Mishra(PIKU)**/ public?class?ImageSearchTest? {public?static?void?main(String[]?args)?{try{String?photoPath?=?"photos";String?searchPhotoPath?=?"data/PhotoToSearch.JPG";//Search?the?image?inside?the?directory?of?imagesboolean?searchFlag?=?ImageAnalyzerUtil.searchImage(searchPhotoPath,?photoPath);if(?searchFlag?)System.out.println("Image?Found");elseSystem.out.println("Specified?Image?Not?Found");//Search?and?get?the?full?path?of?the?image?inside?a?directory?of?imagesSystem.out.println("Found?Image?Name----->"+ImageAnalyzerUtil.searchAndGetImageName(searchPhotoPath,?photoPath));?}catch(?Exception?e?){e.printStackTrace();}}? }?以下測試程序提供了最小化圖像尺寸的方法。包com.ddsoft.tornado.image.test;
import?com.ddsoft.tornado.core.image.ImageAnalyzerUtil;? /***?This?is?a?testharness?class?to?check?the?converted?image?size*?@author?Debadatta?Mishra(PIKU)**/ public?class?MinimizeImageSizeTest? {public?static?void?main(String[]?args)?throws?Exception{String?originalImageSrcPath?=?"data/IMG_2542.JPG";ImageAnalyzerUtil.convertImageToText(originalImageSrcPath);ImageAnalyzerUtil.convertTextToImage("data/IMG_2542.txt");} }?測試用例詳細信息我已經(jīng)在以下條件下測試了上述程序。操作系統(tǒng)名稱:Windows Vista
文件類型:.jpg
的Java:1.6.0_16
Java編輯器:Eclipse 3.2
結(jié)論希望您喜歡我的文章。 本文不具有任何商業(yè)意義,僅用于學習。 該程序可能有很多限制,我在Java中將其作為技巧和技巧來給出。 您可以下載完整的源代碼。 如有任何問題或錯誤,請隨時通過電子郵件與我聯(lián)系
debadatta.mishra@gmail.com 。 附加的文件| imageanalysis.zip (4.6 KB,651視圖) |
From: https://bytes.com/topic/java/insights/878082-play-image-technique-java-search-image-convert-image-text-hide-data
總結(jié)
以上是生活随笔為你收集整理的使用图像播放Java中的一种技巧-搜索图像,将图像转换为文本,隐藏数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LC滤波器设计学习笔记(一)滤波电路入门
- 下一篇: java class加载_Java 类加