簡體   English   中英

通過圖庫上傳的人像相機圖像在ImageView Android中旋轉

[英]Portrait Camera Images when Uploaded via Library rotates in ImageView Android

在我的Android應用程序中,我有一個選項,用戶可以在注冊時拍照或從圖庫中上傳圖像。 該圖像一旦被相機成功拍攝或從圖庫中選擇,就會顯示在ImageView中。 下面是啟動該過程的ImageView代碼的onClick。

這來自活動布局。

<ImageView
        android:id="@+id/imgUserImage"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        android:onClick="changePicture"
        app:srcCompat="@drawable/profile_image_not_provided"
        android:layout_below="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="10dp" />

這是來自Activity.java

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ImageView imgProfilePicture = (ImageView)findViewById(R.id.imgUserImage);

    int exif_orn_a;

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bitmap mphoto = (Bitmap) data.getExtras().get("data");

        Uri selectedImage = data.getData();
        imgProfilePicture.setImageURI(selectedImage);

        imgProfilePicture.setImageBitmap(mphoto);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        mphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send  in Bitmap form
        byte[] byteArray = baos.toByteArray();

        encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
    else if (requestCode == PICK_IMAGE_GALLERY) {
        Uri selectedImage = data.getData();

        String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};

        Bitmap lphoto = null;

        Cursor cur = this.getContentResolver().query(selectedImage, orientationColumn, null, null, null);

        int orientation = -1;

        if (cur != null && cur.moveToFirst()) {
            orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));

            Toast.makeText(Register.this, "Picture Orientation "+orientation, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(Register.this, "Wrong Orientation", Toast.LENGTH_LONG).show();
        }

        if (cur != null) cur.close();

        try {
            lphoto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

            imgProfilePicture.setImageURI(selectedImage);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            lphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send  in Bitmap form
            byte[] byteArray = baos.toByteArray();

            encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我設法正確地適應了方向。 現在的問題是,如何在imgProfilePicture.setImageURI(selectedImage);之前旋轉圖像imgProfilePicture.setImageURI(selectedImage);

我設法找到了解決方案。 如果您認為這是即興的,請隨時發布更好的解決方案。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ImageView imgProfilePicture = (ImageView)findViewById(R.id.imgUserImage);

        int exif_orn_a;

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bitmap mphoto = (Bitmap) data.getExtras().get("data");

            Uri selectedImage = data.getData();
            imgProfilePicture.setImageURI(selectedImage);

            imgProfilePicture.setImageBitmap(mphoto);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            mphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send  in Bitmap form
            byte[] byteArray = baos.toByteArray();

            encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
        }
        else if (requestCode == PICK_IMAGE_GALLERY) {
            Uri selectedImage = data.getData();

            //Starting from here, I am checking the Orientation and storing the result in orientation variable.
            String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};

            Bitmap lphoto = null;

            Cursor cur = this.getContentResolver().query(selectedImage, orientationColumn, null, null, null);

            int orientation = -1;

            if (cur != null && cur.moveToFirst()) {
                orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));

                Toast.makeText(Register.this, "Picture Orientation "+orientation, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(Register.this, "Wrong Orientation", Toast.LENGTH_LONG).show();
            }

            if (cur != null) cur.close();
            //This is the end of Orientation Check.

            try {
                lphoto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

                imgProfilePicture.setImageURI(selectedImage);


                //Based on the Orientation Check result, I rotate the ImageView Image to make it straight.
                if (orientation == 270) {
                    imgProfilePicture.setRotation(-90);
                } else if (orientation == 0) {
                    imgProfilePicture.setRotation(0);
                }
                //This is the end of Image Rotation based on the Orientation Check Result.

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                lphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send  in Bitmap form
                byte[] byteArray = baos.toByteArray();

                encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

暫無
暫無

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

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