【问题标题】:Save rotated Bitmap保存旋转位图
【发布时间】:2015-02-11 11:10:27
【问题描述】:

我从相机拍了一张照片,我只是想以正确的方向保存它。

我知道他们有很多关于该主题的帖子,但即使在阅读了所有答案后,我仍然无法保存旋转的位图。

所以这里是我的代码:

Bitmap bitmap = BitmapFactory.decodeFile(pictureFile
            .getAbsolutePath());
    Matrix matrix = new Matrix();
    matrix.postRotate(finalOrientation);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
    Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

    findViewById(R.id.image_preview).setVisibility(View.VISIBLE);
    ((ImageView) findViewById(R.id.image_preview)).setImageBitmap(rotatedBitmap);
    findViewById(R.id.surfaceView).setVisibility(View.GONE);

    if(outputStream != null)
    {
        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

如您所见,我在图像视图中显示了我的位图图像,以确保它处于正确的方向并且它是正确的。但只有当我保存(压缩)它时,它才会恢复到原来的状态。

提前谢谢你!

【问题讨论】:

  • 我没把你弄好,你能详细说明一下吗?我的意思是你的代码有什么问题?
  • 问题是:我以纵向拍摄照片并将其保存为横向。我想展示的是,当我在 imageView 中显示我的位图时,她的方向正确(在屏幕上,但不在文件资源管理器中)。希望我足够清楚:/
  • 事物和软件实体请使用itheshe 用于人。
  • 我不是母语人士,抱歉弄错了。
  • AFAIK GIF 格式具有控制旋转 N*90 度的属性。相机应用程序曾经支持它(至少在某些设备上),而 Java 类忽略了它。可能就是那个 N*90 特征。

标签: android matrix bitmap camera orientation


【解决方案1】:

不是答案,但我想发布一些代码,可能会阐明正在发生的事情,它不适合评论:

ExifInterface exif;
int rotate = 0;
try {
       exif = new ExifInterface (file.getAbsolutePath());
       int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
       switch (exifOrientation) {
       case ExifInterface.ORIENTATION_ROTATE_90: rotate = 1 /*90*/; break;
       case ExifInterface.ORIENTATION_ROTATE_180: rotate = 2 /*180*/; break;
       case ExifInterface.ORIENTATION_ROTATE_270: rotate = 3 /*270*/; break;
       }
} catch (IOException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
}
Log.d(TAG,"~~~~~~ exifOrientation: rotate="+rotate);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多