简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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