2016-10-20 2 views
0

카메라에서 이미지를 캡처하고 갤러리에서 이미지를 선택합니다. 삼성 전자 기기에서는 캡쳐 후 이미지가 회전합니다.카메라 또는 갤러리에서 이미지를 회전하는 방법.

회전 한 이미지를 똑바로 회전하고 싶습니다.

나는 그것을하려고했지만 그 효과가 없습니다.

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) 
    { 

    } 

} 

편집 :

내가 지금 카메라 의도를 사용하고 의도에서 여전히 작동하지 않는 해당 경로를 얻기 위해 노력했다.

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

지금 뭐야?

아무도 도와 줄 수 있습니까? 무슨 일있어? 감사합니다 ..

+0

가능한 복제를 사용하여 ...

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

회전 비트 맵 활동 결과 방법이 코드를 넣어 이미지를 회전하는 가치를 인트로가 안드로이드의 일부 장치에서 회전하게 됨] (0120-385-331) – earthw0rmjim

+0

You 나를 포함해야한다. thod'modifyOrientation'이이 질문에서 그 방법이 문제라고 생각하기 때문에 – HendraWD

+0

예. 추가하는 것을 잊어 버렸습니다 .. 편집 된 질문을 확인하십시오 .. @Hendra Wijaya Djiono – Sid

답변

0
profileImage = destination; 

비트 맵 인 축소판을 가져 와서 파일에 썼습니다.

그런 다음 해당 파일을 사용하여 exif 인터페이스를 추출합니다.

그러나 비트 맵에는 exif 정보가 없습니다. 따라서 파일도 마찬가지입니다.

오리엔테이션은 항상 null입니다.

를 사용하여 카메라 의도를 시작한 경우 그대로 두십시오.

위의 문을 제거하십시오.

+0

어떤 이미지 경로를 EXIF에 전달해야합니까 ?? 이해 못 하겠어. :-(@greenapps – Sid

+0

제발 .. 당신이 포스터와 이야기하는 것이 분명하기 때문에 매번 @ name으로 답변의 포스터를 말할 필요가 없습니다. – greenapps

+0

'profileImage' 변수를 의도 : 당신은 그 질문에 대답하지 않았습니다. 게시물에 사용 된 의도에 대한 코드를 게시하는 것이 좋습니다. 처음에는 카메라가 촬영 한 이미지의 이미지 경로를 사용해야합니다.섬네일 비트 맵을 저장 한 경로가 아닙니다. – 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; 
} 

그리고 [이미지 캡처 한 이유를 사용하여 카메라의

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

선택한 이미지에 오류가 발생했습니다. 즉, 필요한 문자열이 발견되었습니다. @ Ganesh Pokale – Sid

+0

거기 있습니까? @ 가네시 포 칼레 – Sid

관련 문제