2015-11-06 71 views
1

我想將位圖保存爲.Jpg的存儲。用這種方法成功保存。如何將原始方向的位圖保存爲JPG圖像?

private void SaveImage(Bitmap finalBitmap) { 

String root = Environment.getExternalStorageDirectory().toString(); 
File myDir = new File(root + "/Dir");  

String fname = "Image.jpg"; 
File file = new File (myDir, fname); 
if (file.exists()) file.delete(); 
try { 
     FileOutputStream out = new FileOutputStream(file); 
     finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
     out.flush(); 
     out.close(); 

} catch (Exception e) { 
     e.printStackTrace(); 
} 

} 

但保存的圖像方向是90度clockwised。我想用原來的方向保存它。我該如何解決這個問題。請給我建議。謝謝。

+0

有很多關於SO的帖子,你可以用一點谷歌搜索找到...看到[這個問題](http://stackoverflow.com/a/11081918/2668136)和[這個](http://stackoverflow.com/questions/25231148/android-camera-saving-images-with-wrong-orientation)爲例。 – Fllo

+0

這個問題特別出現在棒棒糖之前的一些android api設備,當用相機捕捉並保存或使用時。他們應該相應地旋轉 – Androider

+0

我想我應該使用shell命令來複制它。 –

回答

1

我希望這會幫助你。

Bitmap asd = Your bitmap image; 
    boolean deleted = false; 
    try 
    {//You can delete here 
     File file = new File("/sdcard/test.png"); 
     deleted = file.delete(); 
    } 
    catch (Exception e) 
    { 

    } 
    OutputStream stream = null; 
    try { 
     stream = new FileOutputStream("/sdcard/test.png"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    asd.compress(Bitmap.CompressFormat.PNG, 100, stream); 
2

您可以試試這個。

首先得到原始圖像

ExifInterface originalImageExif = new ExifInterface(origFile.getAbsolutePath()); 
String origImageOrientation = originalImageExif.getAttribute(ExifInterface.TAG_ORIENTATION); 

然後爲新的形象創造新的Exif數據中的Exif數據中。

ExifInterface exifForNewImage = new ExifInterface(newFile.getAbsolutePath()); 

//Pass the origImageOrientation value that you get from the original image 
exifForNewImage.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation); 

//Then save the Exif attributes 
exifForNewImage.saveAttributes(); 
相關問題