簡體   English   中英

如何將imgeview方向設置為縱向

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

我正在開發一個用於從相機捕獲圖像並從圖庫中獲取圖像的應用程序,該圖像將顯示在“圖像”視圖中,一切正常,但是我的問題是圖像中橫向顯示的方式不正確幫我轉換成縱向的圖像

這是我的代碼

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

這是兩個我用來將位圖旋轉到正確方向的輔助方法。 您只需將要旋轉的位圖及其保存文件位置的絕對路徑傳遞給該方法,即可通過還提供的getRealPathFromURI幫助器獲得該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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM