如何将原始方向的位图保存为JPG图像?
问题描述:
我想将位图保存为.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。我想用原来的方向保存它。我该如何解决这个问题。请给我建议。谢谢。
答
我希望这会帮助你。
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);
答
您可以试试这个。
首先得到原始图像
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();
有很多关于SO的帖子,你可以用一点谷歌搜索找到...看到[这个问题](http://*.com/a/11081918/2668136)和[这个](http://*.com/questions/25231148/android-camera-saving-images-with-wrong-orientation)为例。 – Fllo
这个问题特别出现在棒棒糖之前的一些android api设备,当用相机捕捉并保存或使用时。他们应该相应地旋转 – Androider
我想我应该使用shell命令来复制它。 –