在本文中,我将为您提供一种处理图像文件的方法。 本文将使您深入了解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 file
ImageAnalyzerUtil.convertImageToText(sourceImagePath);
//Convert the image text file back into actual image file
ImageAnalyzerUtil.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 image
ImageAnalyzerUtil.hideTextDataInsideImage(destinationImathPath,
secretData);
//Get back the data hidden inside an image
String 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 images
boolean searchFlag = ImageAnalyzerUtil.searchImage(searchPhotoPath, photoPath);
if( searchFlag )
System.out.println("Image Found");
else
System.out.println("Specified Image Not Found");
//Search and get the full path of the image inside a directory of images
System.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 。