2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 使用图像播放Java中的一种技巧-搜索图像 将图像转换为文本 隐藏数据

使用图像播放Java中的一种技巧-搜索图像 将图像转换为文本 隐藏数据

时间:2021-09-28 03:15:38

相关推荐

使用图像播放Java中的一种技巧-搜索图像 将图像转换为文本 隐藏数据

介绍

在本文中,我将为您提供一种处理图像文件的方法。 本文将使您深入了解Java中的一些技巧,以便您可以隐藏图像内的敏感信息,将完整图像隐藏为文本,在目录内搜索特定图像,并最小化图像的大小。 但是,这不是一个新概念,有一个称为隐写术的概念,可以将您的秘密信息隐藏在图像中。 对于此概念,可以使用许多非常复杂的软件来处理图像。 但作为Java开发人员,我们也可以

在一定程度上实现它。

通常,许多人将所有个人图像保存在特定目录中,其他人则通过搜索* .jpg来查看图像。

它妨碍了您的照片和图像的隐私。 在本文中,我将向您展示如何将图片,照片和图像转换为文本文件以提高隐私性。 其他人可以查看您的文本文件,但看不懂它是什么。 有时,许多用户将所有秘密和敏感信息(例如几个银行详细信息,信用卡号,电子邮件密码详细信息)放在文本文件中,以便他们可以轻松登录特定的应用程序。 但是,当其他人使用这些信息所在的系统时,就会产生问题。 在本文中,我将向您展示如何在图像中隐藏个人详细信息,以及如何在需要时检索详细信息。 让我们进入技术细节,因为我不想延长我的描述。

技术性

在Java中,读取图像非常容易,您可以将图像内容存储为平面文件。 为此,请使用Java中的ImageIO类读取图像文件,并使用Base64Encoder将字节数组转换为String。 现在您的主要工作已经结束,现在您可以以任何方式操作String了。 让我们调查一下案例。

案例– 1:将照片隐藏为文本文件

在这种情况下,您可以将所有照片或图像隐藏到文本文件中,并以其他用户将其视为系统文件或日志的方式重命名文件。 在我的类“ ImageAnalyzerUtil”中,有一种将图像转换为文本文件的方法。 方法名称为“ convertImageToText()”。 在这种方法中,使用“ Base64Encoder”将图像文件读取为字符串,最后将其写入文本文件。

情况– 2:从文本文件中获取原始图像

在“ ImageAnalyzerUtil”类中,有一个名为“ convertTextToImage()”的方法,该方法会将文本文件转换为图像。 但是,所有文本文件都不会转换为图像。 只有那些已从图像文件转换的文本文件才能创建图像。 在这种情况下,将读取转换后的文本文件,并使用“ Base64Decoder”将其转换为字节数组,最后将字节数组写入文件中。

案例– 3:将敏感信息隐藏在图像中

在这种情况下,类“ ImageAnalyzerUtil”中有一个称为“ hideTextDataInsideImage()”的方法。 此处,一个额外的字符串与其他数据一起添加到了图像文件中,以便可以轻松检索数据。

情况– 4:从图像中检索您的敏感信息

为此,在类“ ImageAnalyzerUtil”中有一个称为“ getHiddenDataFromImage()”的方法。 在这种情况下,整个图像将被读取为String,并且敏感信息将被分离并显示。

情况– 5:最小化图像尺寸

在使用该程序时,我发现从文本文件还原原始图像时,图像的大小减小了。 不会丢失图像数据,但是可能会丢失OS提供的某些额外数据。

下面给出了完整的程序以及所有相关的Testharness程序。

packagecom.ddsoft.tornado.core.image;importjava.awt.image.BufferedImage;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.DataInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjavax.imageio.ImageIO;/***Thisclasscontainsthefollowingutilitymethods.*<p>*<li>UtilityMethodtoreadconvertedimagetextfile</li>*<li>Utilitymethodtowritetheimageastextfile</li>*<li>UtilitymethodtogettheimagecontentsasString</li>*<li>Utilitymethodhidethetextdatainsideanimage</li>*<li>Utilitymethodgetthehiddendatafromanimage</li>*<li>Utilitymethodtosearchaparticularimageinsideadirectory</li>*<li>Utilitymethodtosearchandtoobtainthefullpathofanimage</li>*<li>Utilitymethodtoconvertanimageintoatextfile</li>*<li>Utilitymethodtoconvertatextbackintoimage</li>*@authorDebadattaMishra(PIKU)**/publicclassImageAnalyzerUtil{/***Stringtypeconstanttodefinetheimagetype.*/privatestaticStringIMAGE_TYPE="jpg";/***AnextraStringthatseparatestheimagedataandthesecretinformationdata*/privatestaticStringextraStr="@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";/***Thismethodisusedtoreadthecontentsofatext(convertedfromimage)file*andprovidesbyte[].*@paramimageTextFilePathoftypeStringindicatingthepathofthetextfile*@returnanarrayofbytes*@authorDebadattaMishra(PIKU)*/publicstaticbyte[]readImageTextFile(StringimageTextFilePath){byte[]dataFile=null;try{StringBuffertextData=newStringBuffer();FileInputStreamfstream=newFileInputStream(imageTextFilePath);DataInputStreamin=newDataInputStream(fstream);BufferedReaderbr=newBufferedReader(newInputStreamReader(in));StringsrtData;while((srtData=br.readLine())!=null){textData.append(srtData);}br.close();fstream.close();in.close();dataFile=newsun.misc.BASE64Decoder().decodeBuffer(textData.toString());}catch(Exceptione){e.printStackTrace();}returndataFile;}/***Thismethodisusedtowritethecontentsofanimageintoatextfile.*@paramfilePathoftypeStringindicatingthepathofthetextfile*@paramimageContentsoftypeStringindicatingtheimagecontentsasString*@authorDebadattaMishra(PIKU)*/publicstaticvoidwriteImageAsTextFile(StringfilePath,StringimageContents){FileOutputStreamfout=null;OutputStreamWriterosw=null;OutputStreambout=null;try{fout=newFileOutputStream(filePath);bout=newBufferedOutputStream(fout);osw=newOutputStreamWriter(bout,"utf-8");osw.write(imageContents);bout.close();osw.close();fout.close();}catch(Exceptione){e.printStackTrace();}}/***ThismethodisusedtogettheimagecontentsasString.*@paramimagePathoftypeStringindicatingthepathofimagefile*@returnaStringofimagecontents*@authorDebadattaMishra(PIKU)*/publicstaticStringgetImageAsString(StringimagePath){StringimageString=null;try{Filef=newFile(imagePath);BufferedImagebuffImage=ImageIO.read(f);ByteArrayOutputStreamos=newByteArrayOutputStream();ImageIO.write(buffImage,IMAGE_TYPE,os);byte[]data=os.toByteArray();imageString=newsun.misc.BASE64Encoder().encode(data);}catch(FileNotFoundExceptionfnfe){fnfe.printStackTrace();System.out.println("Imageisnotlocatedintheproperpath.");}catch(IOExceptione){e.printStackTrace();System.out.println("Errorinreadingtheimage.");}returnimageString;}/***Thismethodisusedtohidethedatacontentsinsidetheimage.*@paramsrcImagePathoftypeStringindicatingthepathofthesourceimage*@paramdataContentsoftypeStringcontainingdata*@authorDebadattaMishra(PIKU)*/publicstaticvoidhideTextDataInsideImage(StringsrcImagePath,StringdataContents){try{dataContents=newsun.misc.BASE64Encoder().encode(dataContents.getBytes());extraStr=newsun.misc.BASE64Encoder().encode(extraStr.getBytes());FileOutputStreamfos=newFileOutputStream(srcImagePath,true);fos.write(newsun.misc.BASE64Decoder().decodeBuffer(extraStr.toString()));fos.write(dataContents.getBytes());fos.close();}catch(FileNotFoundExceptionfnfe){fnfe.printStackTrace();}catch(IOExceptione){e.printStackTrace();}catch(Exceptione){e.printStackTrace();}}/***Thismethodisusedtogetthehiddendatafromanimage.*@paramimagePathoftypeStringindicatingthepathoftheimage*whichcontainsthehiddendata*@returntheStringcontaininghiddendatainsideanimage*@authorDebadattaMishra(PIKU)*/publicstaticStringgetHiddenDataFromImage(StringimagePath){StringdataContents=null;try{Filefile=newFile(imagePath);byte[]fileData=newbyte[(int)file.length()];InputStreaminStream=newFileInputStream(file);inStream.read(fileData);inStream.close();StringtempFileData=newString(fileData);StringfinalData=tempFileData.substring(tempFileData.indexOf(extraStr)+extraStr.length(),tempFileData.length());byte[]temp=newsun.misc.BASE64Decoder().decodeBuffer(finalData);dataContents=newString(temp);}catch(Exceptione){e.printStackTrace();}returndataContents;}/***Thismethodisusedtosearchaparticularimageinaimagedirectory.*Inthismethod,itwillsearchfortheimagecontentsfortheimage*youarepassing.*@paramimageToSearchoftypeStringindicatingthefilenameoftheimage*@paramimageFolderToSearchoftypeStringindicatingthenameofthedirectory*whichcontainstheimages*@returntrueifimageisfoundelsefalse*@authorDebadattaMishra(PIKU)*/publicstaticbooleansearchImage(StringimageToSearch,StringimageFolderToSearch){booleansearchFlag=false;try{StringsearchPhotoStr=getImageAsString(imageToSearch);Filefiles=newFile(imageFolderToSearch);File[]photosFiles=files.listFiles();for(inti=0;i<photosFiles.length;i++){StringphotoFilePath=photosFiles[i].getAbsolutePath();StringphotosStr=getImageAsString(photoFilePath);if(searchPhotoStr.equals(photosStr)){searchFlag=true;break;}else{continue;}}}catch(Exceptione){e.printStackTrace();}returnsearchFlag;}/***Thismethodisusedtosearchforaparticularimagefoundinadirectory*anditwillreturnthefullpathoftheimagefound.Sometimesitisrequired*tofindouttheparticularimageandthepathoftheimagesothatthepath*Stringcanbeusedforfurtherprocessing.*@paramimageToSearchoftypeStringindicatingthefilenameoftheimage*@paramimageFolderToSearchoftypeStringindicatingthenameofthedirectory*whichcontainstheimages*@returnthefullpathoftheimage*@authorDebadattaMishra(PIKU)*/publicstaticStringsearchAndGetImageName(finalStringimageToSearch,finalStringimageFolderToSearch){StringfoundImageName=null;try{StringsearchPhotoStr=ImageAnalyzerUtil.getImageAsString(imageToSearch);Filefiles=newFile(imageFolderToSearch);File[]photosFiles=files.listFiles();for(inti=0,n=photosFiles.length;i<n;i++){finalStringphotoFilePath=photosFiles[i].getAbsolutePath();finalStringphotosStr=ImageAnalyzerUtil.getImageAsString(photoFilePath);if(searchPhotoStr.equals(photosStr)){foundImageName=photosFiles[i].getAbsolutePath();break;}else{continue;}}}catch(Exceptione){e.printStackTrace();}returnfoundImageName;}/***Thismethodisusedtoconvertanimageintoatextfile.*@paramimagePathoftypeStringindicatingthepathoftheimagefile*@authorDebadattaMishra(PIKU)*/publicstaticvoidconvertImageToText(StringimagePath){Filefile=newFile(imagePath);StringfileName=file.getAbsolutePath();StringtextFilePath=newStringBuilder(fileName.substring(0,fileName.lastIndexOf("."))).append(".txt").toString();writeImageAsTextFile(textFilePath,getImageAsString(imagePath));/**Theremayberequirementtodeletetheoriginalimage,*writethecodehereforthispurpose.*/}/***Thismethodisusedtoconvertthetextfileintoimage.*Howeveralltextfileswillnotbeconvertedintoimages.*Thosetextfileswhichhavebeenconvertedfromimagesfiles*willbeconvertedbackintoimage.*@paramimageTextFilePathoftypeStringindicatingtheconvertedtextfile*fromimagefile.*@authorDebadattaMishra(PIKU)*/publicstaticvoidconvertTextToImage(StringimageTextFilePath){try{Filefile=newFile(imageTextFilePath);StringfileName=file.getAbsolutePath();StringimageFilePath=newStringBuilder(fileName.substring(0,fileName.lastIndexOf("."))).append(".").append(IMAGE_TYPE).toString();OutputStreamout=newFileOutputStream(imageFilePath);byte[]imageBytes=readImageTextFile(imageTextFilePath);out.write(imageBytes);out.close();}catch(Exceptione){e.printStackTrace();}}}

以上是实现图像功能的通用程序。 我在测试用例下方提供了支持上述程序的信息。

以下测试程序提供了将转换为文本文件(反之亦然)的详细信息。

packagecom.ddsoft.tornado.image.test;importcom.ddsoft.tornado.core.image.ImageAnalyzerUtil;/***Thisisatestharnessclasstotestthe*imageconversionutility.*@authorDebadattaMishra(PIKU)**/publicclassConvertImageTest{publicstaticvoidmain(String[]args){StringsourceImagePath="data/IMG_2526.JPG";//ConverttheimageintotextfileImageAnalyzerUtil.convertImageToText(sourceImagePath);//ConverttheimagetextfilebackintoactualimagefileImageAnalyzerUtil.convertTextToImage("data/IMG_2526.txt");}}

下面的testharness程序演示了如何在图像内部隐藏数据。

packagecom.ddsoft.tornado.image.test;importcom.ddsoft.tornado.core.image.ImageAnalyzerUtil;/***Thisisatestharnessclasstohidetheinformationinsideanimage*@authorDebadattaMishra(PIKU)**/publicclassHideDataTest{publicstaticvoidmain(String[]args){StringsecretData="Itcontainsallmysecretmaterialsandmybankinformationdetails";StringdestinationImathPath="data/secretImage.jpg";//HidetheinformationinsidetheimageImageAnalyzerUtil.hideTextDataInsideImage(destinationImathPath,secretData);//GetbackthedatahiddeninsideanimageStringhiddenData=ImageAnalyzerUtil.getHiddenDataFromImage(destinationImathPath);System.out.println(hiddenData);}}

以下测试程序显示了如何在图像目录中搜索图像。

packagecom.ddsoft.tornado.image.test;importcom.ddsoft.tornado.core.image.ImageAnalyzerUtil;/***Thisisatestharnessclasstosearchforanimage*@authorDebadattaMishra(PIKU)**/publicclassImageSearchTest{publicstaticvoidmain(String[]args){try{StringphotoPath="photos";StringsearchPhotoPath="data/PhotoToSearch.JPG";//SearchtheimageinsidethedirectoryofimagesbooleansearchFlag=ImageAnalyzerUtil.searchImage(searchPhotoPath,photoPath);if(searchFlag)System.out.println("ImageFound");elseSystem.out.println("SpecifiedImageNotFound");//SearchandgetthefullpathoftheimageinsideadirectoryofimagesSystem.out.println("FoundImageName----->"+ImageAnalyzerUtil.searchAndGetImageName(searchPhotoPath,photoPath));}catch(Exceptione){e.printStackTrace();}}}

以下测试程序提供了最小化图像尺寸的方法。

包com.ddsoft.tornado.image.test;

importcom.ddsoft.tornado.core.image.ImageAnalyzerUtil;/***Thisisatestharnessclasstochecktheconvertedimagesize*@authorDebadattaMishra(PIKU)**/publicclassMinimizeImageSizeTest{publicstaticvoidmain(String[]args)throwsException{StringoriginalImageSrcPath="data/IMG_2542.JPG";ImageAnalyzerUtil.convertImageToText(originalImageSrcPath);ImageAnalyzerUtil.convertTextToImage("data/IMG_2542.txt");}}

测试用例详细信息我已经在以下条件下测试了上述程序。

操作系统名称:Windows Vista

文件类型:.jpg

的Java:1.6.0_16

Java编辑器:Eclipse 3.2

结论

希望您喜欢我的文章。 本文不具有任何商业意义,仅用于学习。 该程序可能有很多限制,我在Java中将其作为技巧和技巧来给出。 您可以下载完整的源代码。 如有任何问题或错误,请随时通过电子邮件与我联系

debadatta.mishra@ 。

From: /topic/java/insights/878082-play-image-technique-java-search-image-convert-image-text-hide-data

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。