【问题标题】:encoding and decoding in JPG images in AndroidAndroid中JPG图像的编码和解码
【发布时间】:2012-05-01 08:04:05
【问题描述】:

我正在通过 Android 创建一个应用程序,我需要在其中处理我的 JPG 文件。我没有获得太多 JPG 格式的标题信息,因此我将其转换为位图,操作位图中的像素值,然后再次将其转换回 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 像素值,请帮助我...

【问题讨论】:

    标签: java android jpeg steganography


    【解决方案1】:

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

    【讨论】:

    • 是的,但我需要将其保存在 JPG 文件中,因为大多数移动用户使用 JPG 图像文件....有没有办法我可以在 JPG 中完成所有编码 n 仍然能够检索所有数据?
    • 取决于您对 检索所有数据 的含义。默认情况下,JPEG 是一种有损格式,只有少数操作(例如裁剪和旋转)可以在不丢失 JPEG 更多信息的情况下完成。至少如果您使用 SDK 提供的方法打开并保存图像,您将丢失信息。
    【解决方案2】:

    谨慎使用

    Bitmap bi = BitmapFactory.decodeStream(fis);
    bi = bi.copy(Bitmap.Config.ARGB_8888, true);
    

    在您拥有第一个bi 时,您可能已经丢失了很多信息,请尝试使用BitmapFactory.Options 强制8888(这也是默认设置):

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = false;
    Bitmap bi = BitmapFactory.decodeStream(fis, options);
    

    如果你留在copy,你应该还是recycle()你扔掉的那个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2014-02-01
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多