728x90
반응형
질문 : 카메라 인 텐트를 사용하여 캡처 한 이미지가 Android의 일부 장치에서 회전되는 이유는 무엇입니까?
이미지를 캡처하여 이미지보기로 설정하고 있습니다.
public void captureImage() {
Intent intentCamera = new Intent("android.media.action.IMAGE_CAPTURE");
File filePhoto = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
imageUri = Uri.fromFile(filePhoto);
MyApplicationGlobal.imageUri = imageUri.getPath();
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intentCamera, TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentFromCamera) {
super.onActivityResult(requestCode, resultCode, intentFromCamera);
if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
if (intentFromCamera != null) {
Bundle extras = intentFromCamera.getExtras();
if (extras.containsKey("data")) {
bitmap = (Bitmap) extras.get("data");
}
else {
bitmap = getBitmapFromUri();
}
}
else {
bitmap = getBitmapFromUri();
}
// imageView.setImageBitmap(bitmap);
imageView.setImageURI(imageUri);
}
else {
}
}
그러나 문제는 이미지가 회전 할 때마다 일부 장치의 이미지가 표시된다는 것입니다. 예를 들어 삼성 기기에서는 잘 작동하지만 Sony Xperia 에서는 이미지가 90도 회전하고 Toshiba Thrive (태블릿)에서는 180도 회전됩니다.
답변
대부분의 휴대폰 카메라는 가로 모드이므로 세로로 사진을 찍으면 결과 사진이 90도 회전됩니다. 이 경우 카메라 소프트웨어는 사진을 볼 방향으로 Exif 데이터를 채워야합니다.
아래 솔루션은 Exif 데이터를 채우는 카메라 소프트웨어 / 장치 제조업체에 따라 다르므로 대부분의 경우 작동하지만 100 % 신뢰할 수있는 솔루션은 아닙니다.
ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap = null;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = bitmap;
}
다음은 rotateImage
메서드입니다.
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
출처 : https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices-on-a
728x90
반응형
'프로그래밍 언어 > Android' 카테고리의 다른 글
Android에서 한 활동에서 다른 활동으로 객체를 전달하는 방법 (0) | 2021.10.14 |
---|---|
Android 튜토리얼에서 대부분의 필드 (클래스 멤버)가 'm'으로 시작하는 이유 (0) | 2021.10.14 |
Android의 ListView에서 이미지를 지연 로드하는 방법 (0) | 2021.10.13 |
Android ADB 도구를 사용하여 애플리케이션을 시작하는 방법 (0) | 2021.10.13 |
내 Android 애플리케이션에서 충돌 데이터를 얻는 방법 (0) | 2021.10.12 |