2017-03-26 7 views
0

私のAndroidアプリでは、登録時に写真を撮ったりギャラリーから画像をアップロードしたりすることができます。この画像は、カメラまたはギャラリーから選択すると正常に撮影された後、ImageViewに表示されます。以下は、このプロセスを開始するImageViewコードのonClickです。ライブラリーを経由してアップロードしたときの肖像カメラ画像はImageView Androidで回転します

これはアクティビティレイアウトのものです。

<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);の前にイメージをどのように回転させるべきかということです。

答えて

0

解決策を見つけることができました。これが即興にできると思えば、より良い解決策を投稿してください。

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(); 
      } 
     } 
    } 
関連する問題