我正在Android上創建一個應用程序,我需要操縱我的JPG文件。我沒有獲得JPG格式的標題信息,因此我將它轉換爲位圖,處理位圖中的像素值,然後再將其轉換回JPG。在Android的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像素值...
是的,但我需要將它保存爲JPG文件,因爲大多數移動用戶使用JPG圖像文件....有沒有辦法,我可以做所有我的編碼在JPG n仍然能夠檢索所有數據? – Tani 2012-04-19 19:00:40
取決於您使用_retrieve查看所有data_的含義。默認情況下,JPEG是一種有損格式,只需少量操作(例如裁剪和旋轉)即可完成,而不會損失JPEG的更多信息。至少如果您使用SDK提供的方法打開並保存圖像,您將失去信息。 – Niels 2012-04-19 19:25:33