介绍

在本文中,我将为您提供一种处理图像文件的方法。 本文将使您深入了解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程序。


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();}}
} 

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

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


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程序演示了如何在图像内部隐藏数据。


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");}
} 

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

操作系统名称:Windows Vista

文件类型:.jpg

的Java:1.6.0_16

Java编辑器:Eclipse 3.2

结论

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

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

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

  1. java中的复合数据类型是什么_【填空题】类是Java中的一种重要的复合数据类型,是组成Java程序的基本要素。一个类的实现包括两部分:____和_____....

    [填空题]类是Java中的一种重要的复合数据类型,是组成Java程序的基本要素.一个类的实现包括两部分:____和_____. 更多相关问题 [名词解释] 观叶树木 [单选] 开花时有浓郁香气的树种是 ...

  2. Java 中的四种引用

    垃圾收集器与内存分配策略参考目录: 1.判断Java 对象实例是否死亡 2. Java 中的四种引用 3.垃圾收集算法 4. Java9中的GC 调优 5.内存分配与回收策略 在进行垃圾回收之前,虚拟 ...

  3. 分析Java中的三种不同变量的区别

    1.首先分析Java中的三种不同变量的区别,如下表所示   概念 默认值 其他 类变量 也叫静态变量,是类中独立于方法之外的变量 用static 修饰 有默认初始值,系统自动初始化. 如boolean ...

  4. java中的五种排序方法_用Java排序的五种有用方法

    java中的五种排序方法 Java排序快速概述: 正常的列表: private static List VEGETABLES = Arrays.asList("apple", &q ...

  5. java类型转换答案,在java中支持两种类型的类型转换,自动类型转换和强制类型转换。父类转化为子类需要强制转换。...

    在java中支持两种类型的类型转换,自动类型转换和强制类型转换.父类转化为子类需要强制转换. 更多相关问题 计算机病毒通过()传染扩散得极快,危害最大. 当一个现象的数量由小变大,另一个现象的数量相反 ...

  6. Java中的两种异常类型及其区别?

    Java中的两种异常类型及其区别? 参考文章: (1)Java中的两种异常类型及其区别? (2)https://www.cnblogs.com/zxfei/p/11182730.html (3)htt ...

  7. <随笔03>Java中的两种异常类型

    <随笔03>Java中的两种异常类型 参考文章: (1)<随笔03>Java中的两种异常类型 (2)https://www.cnblogs.com/newlyfly/p/744 ...

  8. java中的四种代码块

    原文链接: java中的四种代码块_Munt的博客-CSDN博客_java中代码块 在java中用{}括起来的称为代码块,代码块可分为以下四种: 一.简介 1.普通代码块: 类中方法的方法体 2.构造 ...

  9. java中的7种单例模式

    java中的7种单例模式 单例模式是我们开发中经常会用到的,单例模式的好处是应用在运行时只有一个实例,生命周期从单例实例化后一直到应用生命周期结束.这里总结和比较一下几种单例的形式,共总结了七种. 写 ...

最新文章

  1. Transformer不比CNN强!Local Attention和动态Depth-wise卷积的前世今生
  2. linux系统运行flash3d,真正的3D操作系统,太强了
  3. 使用Silverlight Toolkit绘制图表(上)--柱状图
  4. 三十分钟掌握STL(Using STL)
  5. SAP Spartacus B2B Org Unit树状结构的ghost数据
  6. php5 数据库框架,数据库 · FastAdmin - 基于ThinkPHP5的极速后台开发框架文档 · 看云...
  7. socket_基础2_传输大数据
  8. java 日期加一天_漫话:如何给女朋友解释为什么一到年底,部分网站就会出现日期混乱的现象?...
  9. python怎么测试uwsgi并发量_nginx + uWSGI 为 django 提供高并发
  10. iPhone 13拍照马赛克、换屏无法解锁Face ID、iPad mini 6“果冻屏”:等“百香果”吧...
  11. error This module isn‘t specified in a package.json file.
  12. Oracle SQL 优化原则(实用篇)
  13. 移动开发者走向全能开发者的五大技能
  14. 导致存储过程重新编译的原因
  15. 教你计算三种分子性质的方法
  16. 利用密码字典暴力破解渗透目标系统
  17. 未能成功连接停车场服务器,停车场管理系统常见问题解答
  18. 心不唤物,物不至,聊聊积极心态重要性
  19. 创意爆破效果PS动作
  20. 2021-07-15-2021年全球10大最佳单板计算机开发板(SBC)(第1-3名)

热门文章

  1. 在国际化中如何获取当前浏览器的语种
  2. 匮乏即是富足,自律产生喜悦_当惊喜与喜悦分开时
  3. 幅相曲线渐近线_若最小相位系统的低频段幅频特性的渐近线是一条斜率为20dB/dec的直线,则该系统( )。_学小易找答案...
  4. 夏普电视显示网络无法连接到服务器,彻底解决SQL SERVER 2005无法远程连接的问题...
  5. (转)Google Voice呼转到中国电话的五种方法
  6. 工具栏的打印图标不见了_电脑工具栏图标不见了怎么办啊
  7. Java is Pass-by-Value, Dammit!
  8. 美光科技面试经验总结(2017-1-6)
  9. 视频眼镜中微显示器技术:LCD、LCoS、OLED和MEMS
  10. 智慧公寓管理系统解决方案