【问题标题】:How to save Bitmap to Jpg image with original orientation?如何将位图保存为原始方向的 Jpg 图像?
【发布时间】:2015-11-06 08:24:28
【问题描述】:

我正在尝试将位图保存为 .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 度。我想以原始方向保存它。我该如何解决这个问题。请给我建议。谢谢你。

【问题讨论】:

  • 有很多关于 SO 的帖子,您可以通过一点 Google 搜索找到...例如,请参阅 this questionthis one
  • 这个问题特别出现在一些棒棒糖之前的android api设备上,当被相机捕捉并保存或使用时。它们应该相应地旋转
  • 我想我应该使用shell命令来复制它。

标签: android bitmap


【解决方案1】:

你可以试试这个。

先获取原图的Exif数据

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

然后为新图像创建新的 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();

【讨论】:

    【解决方案2】:

    希望对你有帮助。

    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);
    

    【讨论】:

    • 您无法使用 .png 访问 ExifData
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2012-08-13
    • 1970-01-01
    • 2014-02-08
    • 2014-03-21
    相关资源
    最近更新 更多