Created
July 30, 2014 21:20
-
-
Save weitsai/2cf4679b9cd660182048 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public CAMERA_TEST extends Fragment { | |
private final int CAMERA_IMAGE_CODE = 10; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
File out = new File(Environment.getExternalStorageDirectory(), "test.jpg"); | |
Uri uri = Uri.fromFile(out); | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); | |
startActivityForResult(intent, CAMERA_IMAGE_CODE); | |
super.onCreate(savedInstanceState); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == CAMERA_IMAGE_CODE) { | |
File file = new File(Environment.getExternalStorageDirectory(), "test.jpg"); | |
// 有些手機回傳會是 0 | |
Log.i("imageRotation", getExifRotation(file)); | |
} | |
} | |
public static int getExifRotation(File imageFile) { | |
if (imageFile == null) { | |
return 0; | |
} | |
try { | |
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); | |
// We only recognize a subset of orientation tag values | |
switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)) { | |
case ExifInterface.ORIENTATION_ROTATE_90: | |
return 90; | |
case ExifInterface.ORIENTATION_ROTATE_180: | |
return 180; | |
case ExifInterface.ORIENTATION_ROTATE_270: | |
return 270; | |
default: | |
return ExifInterface.ORIENTATION_UNDEFINED; | |
} | |
} catch (IOException e) { | |
Log.e("Error getting Exif data", e); | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment