在Android中的JPG图像中编码和解码

时间:2012-04-19 18:19:42

标签: java android jpeg steganography

我正在Android上创建一个应用程序,我需要操作我的JPG文件。我没有得到很多JPG格式的标题信息,因此我将其转换为Bitmap,操纵位图中的像素值,然后再将其转换回JPG。

我面临的问题是 - 仅操纵位图和像素的某些像素 将它转换回JPG,我没有得到我之前得到的相同像素集(对于那些我没有操作的像素)。我在新图像中获得与原始图像相同的图像。但是当我检查用于解码的新图像像素值时,未触摸的像素是不同的......

File imagefile = new File(filepath);
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis);
int intArray[];
bi=bi.copy(Bitmap.Config.ARGB_8888,true);       
intArray = new int[bi.getWidth()*bi.getHeight()];
bi.getPixels(intArray, 0, bi.getWidth(), 0, 0, bi.getWidth(), bi.getHeight());

int newArray[] = encodeImage(msgbytes,intArray,mbytes); // method where i am manipulating my pixel values 

// converting the bitmap data back to JPG file
bi = Bitmap.createBitmap(newArray, bi.getWidth(), bi.getHeight(), Bitmap.Config.ARGB_8888);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();

Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
String filepath = "/sdcard/image/new2.jpg";
File imagefile = new File(filepath);
FileOutputStream fos = new FileOutputStream(imagefile);
bitmapimage.compress(CompressFormat.JPEG, 100, fos);

如果我在某处错了,或者我是否应该使用其他方法来操纵JPG像素值,请帮助我......

1 个答案:

答案 0 :(得分:3)

JPEG是一种通常基于lossy compression的图像格式。这意味着一些对人眼不重要的信息会被丢弃以进一步缩小文件大小。尝试将图像保存为PNG(无损格式)。