如何将imgeview Orientation设置为portrait

时间:2015-10-25 23:58:04

标签: android

我正在开发一个应用程序,用于从相机中捕获图像并从图库中获取图像,图像将显示在图像视图中,每件事情都能正常工作但我的问题在于图像中横向不能以正确的方式出现任何人都可以帮我转换为纵向图像

这是我的代码

    open.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, REQUEST_CODE);
        }
    });

camera.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });



 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {

            if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = getContentResolver().openInputStream(
                    data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            pic.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    try {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            if (bitmap != null) {
                bitmap.recycle();
            }
            bitmap = (Bitmap) data.getExtras().get("data");
            pic.setImageBitmap(bitmap);
        }
    }
 catch (Exception e) {
    e.printStackTrace();
}
}

1 个答案:

答案 0 :(得分:2)

以下是两种辅助方法,用于将位图旋转到正确的方向。您只需将要旋转的位图以及保存文件位置的绝对路径传递给方法,也可以使用提供的getRealPathFromURI帮助程序获取,并且您将收到正确旋转的位图。

您的代码

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    if (bitmap != null) {
        bitmap.recycle();
    }
    bitmap = (Bitmap) data.getExtras().get("data");
    String absPath = getRealPathFromURI(data.getData());
    Bitmap rotatedBitmap = rotateBitmap(absPath, bitmap);
    pic.setImageBitmap(rotatedBitmap);
}

<强>助手

/**
 * Converts a Uri to an absolute file path
 *
 * @param contentUri Uri to be converted
 * @return String absolute path of Uri
 */
public String getRealPathFromURI(Uri contentUri) {

    // Can post image
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = this.getContentResolver().query(contentUri,
            proj,  // Which columns to return
            null,  // WHERE clause; which rows to return (all rows)
            null,  // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();

    return cursor.getString(column_index);
}


/**
 * Rotates the bitmap to the correct orientation for displaying
 *
 * @param filepath absolute path of original file
 * @param originalBitmap original bitmap to be rotated
 * @return
 */
private Bitmap rotateBitmap(String filepath, Bitmap originalBitmap) {
    try {
        // Find the orientation of the original file
        ExifInterface exif = new ExifInterface(filepath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        Matrix matrix = new Matrix();

        // Find correct rotation in degrees depending on the orientation of the original file
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.postRotate(90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.postRotate(180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.postRotate(270);
                break;
            default:
                return originalBitmap;
        }

        // Create new rotated bitmap
        Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                originalBitmap.getHeight(), matrix, true);

        return rotatedBitmap;

    } catch (IOException e) {
        Log.d(TAG, "File cannot be found for rotating.");
        return originalBitmap;
    }
}