2016-10-20 6 views
0

Ich nehme Bild von der Kamera auf und wähle Bild von der Galerie aus. In Samsung-Geräten werden die Bilder nach der Aufnahme gedreht.Wie Bild von Kamera oder Galerie gedreht.?

Ich möchte Bild drehen, wenn sie gedreht werden.

Ich habe versucht, es zu tun, aber es funktioniert nicht.

private void onCaptureImageResult(Intent data) { 
    try { 

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes); 

    File destination = new File(Environment.getExternalStorageDirectory(), 
      System.currentTimeMillis() + ".png"); 

    FileOutputStream fo; 

     destination.createNewFile(); 
     fo = new FileOutputStream(destination); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 


    profileImage = destination; 

    Bitmap rotatedBitmap = modifyOrientation(thumbnail, profileImage.getAbsolutePath()); 

    ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); 
    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage. 
    byte[] byteArray1 = stream1.toByteArray(); 

    File tempFile1 = File.createTempFile("temp", null, getCacheDir()); 
    FileOutputStream fos1 = new FileOutputStream(tempFile1); 
    fos1.write(byteArray1); 


    if (rotatedBitmap != null) { 
     profileImageView.setImageBitmap(rotatedBitmap); 
     profileImage = tempFile1; 
    } else { 
     profileImageView.setImageBitmap(thumbnail); 
     profileImage = destination; 
    } 

} 
    catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

@SuppressWarnings("deprecation") 
private void onSelectFromGalleryResult(Intent data) { 

    Bitmap bm=null; 
    if (data != null) { 
     try { 
      bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData()); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage. 
    byte[] byteArray = stream.toByteArray(); 

    try { 

     File tempFile = File.createTempFile("temp",null, getCacheDir()); 
     FileOutputStream fos = new FileOutputStream(tempFile); 
     fos.write(byteArray); 

     profileImage = tempFile; 

     Bitmap rotatedBitmap = modifyOrientation(bm,profileImage.getAbsolutePath()); 

     ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage. 
     byte[] byteArray1 = stream1.toByteArray(); 

     File tempFile1 = File.createTempFile("temp",null, getCacheDir()); 
     FileOutputStream fos1 = new FileOutputStream(tempFile1); 
     fos1.write(byteArray1); 



     if(rotatedBitmap != null) { 
      profileImageView.setImageBitmap(rotatedBitmap); 
      profileImage = tempFile1; 
     } 
     else { 
      profileImageView.setImageBitmap(bm); 
      profileImage = tempFile; 
     } 
    } 

    catch (IOException e) 
    { 

    } 

} 

EDIT:

Ich versuchte nun Kamera Absicht zu verwenden und den Pfad von der Absicht seiner noch erhalten nicht funktioniert.

private void onCaptureImageResult(Intent data) { 
    try { 

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes); 

    File destination = new File(Environment.getExternalStorageDirectory(), 
      System.currentTimeMillis() + ".png"); 

    FileOutputStream fo; 

     destination.createNewFile(); 
     fo = new FileOutputStream(destination); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
     Bitmap rotatedBitmap = null; 

    // profileImage = destination; 

     Uri tempUri = getImageUri(getApplicationContext(),thumbnail); 

     // CALL THIS METHOD TO GET THE ACTUAL PATH 
     File finalFile = new File(getRealPathFromURI(tempUri)); 

     ExifInterface ei = new ExifInterface(finalFile.getAbsolutePath()); 
     int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_UNDEFINED); 

     switch(orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       rotateImage(thumbnail, 90); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       rotateImage(thumbnail, 180); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       rotateImage(thumbnail, 270); 
       break; 
      case ExifInterface.ORIENTATION_NORMAL: 
      default: 
       break; 
     } 

    if (rotatedBitmap != null) { 

     profileImageView.setImageBitmap(rotatedBitmap); 

     ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); 
     rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage. 
     byte[] byteArray1 = stream1.toByteArray(); 

     File tempFile1 = File.createTempFile("temp", null, getCacheDir()); 
     FileOutputStream fos1 = new FileOutputStream(tempFile1); 
     fos1.write(byteArray1); 

     profileImage = tempFile1; 
    } else { 
     profileImageView.setImageBitmap(thumbnail); 
     profileImage = destination; 
    } 

} 
    catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 


public Uri getImageUri(Context inContext, Bitmap inImage) { 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes); 
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); 
    return Uri.parse(path); 
} 

public String getRealPathFromURI(Uri uri) { 
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
} 



    @Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == SELECT_FILE) 
      onSelectFromGalleryResult(data); 
     else if (requestCode == REQUEST_CAMERA) 
      onCaptureImageResult(data); 
    } 
} 

Was ist jetzt los?

Kann mir bitte jemand helfen? Was läuft falsch? Danke ..

+0

Möglichen Duplikat [warum Bild aufgenommen mit der Kamera Absicht wird auf einigen Geräten in Android gedreht] (http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android) – earthw0rmjim

+0

Sie sollte mich einschließen thod 'modifyOrientation' in dieser Frage, da ich denke, dass diese Methode das Problem ist – HendraWD

+0

ja ich habe vergessen, es hinzuzufügen .. Bitte überprüfen Sie die bearbeitete Frage .. @Hendra Wijaya Djiono – Sid

Antwort

0
profileImage = destination; 

Sie haben das Thumbnail genommen, das eine Bitmap ist und es in Datei geschrieben.

Danach haben Sie diese Datei verwendet, um ein Exifinterface zu extrahieren.

Aber Bitmaps enthalten keine Exif-Informationen. Und daher auch Ihre Datei nicht.

So ist Ihre Ausrichtung immer Null;

Wenn Sie die Kameravorgabe profileImage verwendet haben, lassen Sie sie unverändert.

So entfernen Sie obige Aussage.

+0

Welcher Bildpfad muss an EXIF ​​übergeben werden? Ich verstehe es nicht. :-(@greenapps – Sid

+0

Bitte .. Sie müssen nicht das Poster einer Antwort mit @ Namen jedes Mal adressieren, da es offensichtlich ist, dass Sie mit dem Plakat sprechen. – greenapps

+0

Ich fragte Sie, ob Sie 'profileImage' Variable mit Ihrem verwendet hatten Absicht: Sie haben diese Frage nicht beantwortet. Sie geben besser den Code für die verwendete Absicht in Ihrem Beitrag ein. Am Anfang müssen Sie den Bildpfad des von der Kamera aufgenommenen Bildes verwenden.Kein Pfad, in dem Sie die Miniaturansicht gespeichert haben, wie Sie es jetzt tun. – greenapps

0
public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){ 
int rotate = 0; 
try { 
    context.getContentResolver().notifyChange(imageUri, null); 
    File imageFile = new File(imagePath); 

    ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); 
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

    switch (orientation) { 
    case ExifInterface.ORIENTATION_ROTATE_270: 
     rotate = 270; 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_180: 
     rotate = 180; 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_90: 
     rotate = 90; 
     break; 
    } 

    Log.i("RotateImage", "Exif orientation: " + orientation); 
    Log.i("RotateImage", "Rotate value: " + rotate); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
return rotate; 
} 

Und setzen Sie diesen Code in Folge Methode Aktivität und bekommt Wert Bild zu drehen ...

String selectedImage = data.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

     Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     filePath = cursor.getString(columnIndex); 
     cursor.close(); 

     int rotateImage = getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath); 

Drehen Bitmap mit diesem

public static Bitmap rotate(Bitmap bitmap, float degrees) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(degrees); 
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
    } 
+0

sein Geben Fehler an selectedImage, d. H. Erforderliche Zeichenfolge gefunden URI. @ Ganesh Pokale – Sid

+0

Sind Sie da? @ Ganesh Pokale – Sid

Verwandte Themen