2017-03-26 1 views
0

In meiner Android App habe ich eine Option, wo ein Benutzer zum Zeitpunkt der Registrierung sein Foto machen oder ein Bild von seiner Galerie hochladen kann. Dieses Bild, das erfolgreich von der Kamera aufgenommen oder aus der Galerie ausgewählt wurde, wird in einem ImageView angezeigt. Unten ist der OnClick von ImageView-Code, der diesen Prozess startet.Hochformat Kamerabilder beim Hochladen über Bibliothek rotiert in ImageView Android

Dies ist von Activity Layout.

<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" /> 

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

ich es geschafft haben, richtig die Orientations zu bekommen. Jetzt ist das Problem, wie sollte ich das Bild vor imgProfilePicture.setImageURI(selectedImage); drehen.

Antwort

0

Ich habe es geschafft, die Lösung zu finden. Bitte zögern Sie nicht, eine bessere Lösung zu veröffentlichen, wenn Sie denken, dass dies improvisiert werden kann.

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