简体   繁体   English

如何将imgeview方向设置为纵向

[英]How to set the imgeview Orientation to be portrait

I 'm working on an application to capture image from camera and get image from gallery the images will be shown on Image view every thing is work fine but my problem is in the image that in landscape orientation not appear in the right way can any one help me to convert the image in portrait orientation 我正在开发一个用于从相机捕获图像并从图库中获取图像的应用程序,该图像将显示在“图像”视图中,一切正常,但是我的问题是图像中横向显示的方式不正确帮我转换成纵向的图像

this is my code 这是我的代码

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

Here are two helper methods that I use to rotate bitmaps to the correct orientation. 这是两个我用来将位图旋转到正确方向的辅助方法。 You simply pass the method the bitmap you would like to rotate along with the absolute path to it's saved file location, which can be obtained with the getRealPathFromURI helper also supplied, and you will receive a correctly rotated bitmap. 您只需将要旋转的位图及其保存文件位置的绝对路径传递给该方法,即可通过还提供的getRealPathFromURI帮助器获得该getRealPathFromURI ,您将收到正确旋转的位图。

Your code 您的密码

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

Helpers 助手

/**
 * 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM