imageView中的照片方向与三星手机

时间:2017-02-17 07:23:38

标签: java android imageview samsung-mobile onactivityresult

我在三星设备上的图像有问题 每次我拍摄垂直形式的照片,但当它调用画廊的图像时,教室 垂直 它用类ExifInterface编程,我找不到结果

每次拍照时我都会调用我的类PreviewImage,这是我显示图像的地方 随后我有一个按钮,让我分配配置文件照片我有这个方法onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        try {
            ExifInterface exifInterface = new ExifInterface(photoFile.getAbsolutePath());
            int exif = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (exif) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    orientation = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    orientation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    orientation = 270;
                    break;
            }
        } catch (Exception e) {
            Log.d("tmessages", e.toString());
        }
        Intent intent = new Intent(this, PhotoViewer.class);
        intent.putExtra(EXTRA_PHOTO_PATH, photoFile.getAbsolutePath());
        intent.putExtra(EXTRA_PHOTO_ORIENTATION, orientation);
        intent.putExtra(EXTRA_PHOTO_EDIT, false);
        startActivityForResult(intent, 10);

    } 

这是我的PhotoViewer类

   private void sendPicture() {
        Intent returnIntent = new Intent();  
        returnIntent.putExtra("photoDescription", photoDescription.getText().toString());
        returnIntent.putExtra("photoPath", localPath);
        returnIntent.putExtra("orientation", orientation);
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }

拍完照片后,它会返回到我们调用它的Profile类的onActivityResult方法

else if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
        if (photoFile != null&&LoginController.getInstance().xmppConnection.isConnected()) {
            byte[] photo = AndroidUtilities
                    .createProfileImage(photoFile.getAbsolutePath());
            ProfileManager.getInstance().publishPhoto( photo);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(photo, 0, photo.length);
            avatar.setImageBitmap(decodedByte);
        }else{
            Toast.makeText(this,R.string.LabelProfilePhotoFailed, Toast.LENGTH_SHORT).show();
        }
    }

如何让图片在个人资料照片中显示为垂直?

1 个答案:

答案 0 :(得分:0)

你完成了一半的工作!只需使用您之前计算的方向:

else if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
    if (photoFile != null&&LoginController.getInstance().xmppConnection.isConnected()) {
        byte[] photo = AndroidUtilities
                .createProfileImage(photoFile.getAbsolutePath());
        ProfileManager.getInstance().publishPhoto( photo);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(photo, 0, photo.length);
 ->     decodedByte = rotate(decodedByte, orientation);
        avatar.setImageBitmap(decodedByte);
    }else{
        Toast.makeText(this,R.string.LabelProfilePhotoFailed, Toast.LENGTH_SHORT).show();
    }
}

方向 = getIntent()。getExtras()。getInt(EXTRA_PHOTO_ORIENTATION)。

private static Bitmap rotate(Bitmap bm, int rotation) {
    if (rotation != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        Bitmap bmOut = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        return bmOut;
    }
    return bm;
}