Ich versuche, Microsofts ProjektOxford EmotionApi Bild-Auto-Rotator-Code anzupassen. Jedes von der Gerätekamera aufgenommene Bild wird auf seinen Winkel hin analysiert und dann in die korrekte Landschaftsansicht gedreht, um von der Emotion-API analysiert zu werden.Eine Bitmap drehen, ohne einen ImageURI/ContentResolver zu verwenden?
Meine Frage ist: Wie würde ich den folgenden Code anpassen, um ein Bitmap als Argument zu nehmen? Ich bin auch in Bezug auf die Rolle von Content Resolver und ExitInterface in diesem Fall völlig verloren. Jede Hilfe wird sehr geschätzt.
private static int getImageRotationAngle(
Uri imageUri, ContentResolver contentResolver) throws IOException {
int angle = 0;
Cursor cursor = contentResolver.query(imageUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
if (cursor != null) {
if (cursor.getCount() == 1) {
cursor.moveToFirst();
angle = cursor.getInt(0);
}
cursor.close();
} else {
ExifInterface exif = new ExifInterface(imageUri.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
angle = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
angle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
angle = 90;
break;
default:
break;
}
}
return angle;
}
// Rotate the original bitmap according to the given orientation angle
private static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
// If the rotate angle is 0, then return the original image, else return the rotated image
if (angle != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(
bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} else {
return bitmap;
}
}
Verstanden, danke für die gründliche Erklärung. Irgendwelche Empfehlungen zum Drehen einer Bitmap? –
@ A.Xu: Nun, die 'rotateBitmap()' Methode in Ihrer Frage wird die Bitmap rotieren. Der Punkt hinter dem gesamten EXIF-Header ist, festzustellen, ob ein JPEG gedreht werden sollte und wenn ja, um wie viel. – CommonsWare