まず、以下のスニペットを使用してカメラの向きを確認します。
private int lookupRotation() {
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display mDisplay = mWindowManager.getDefaultDisplay();
int rotation = mDisplay.getRotation();
Log.v(LOG_TAG, "rotation: " + rotation);
return rotation;
}
次に、次を使用して目的の回転を確認し、方向を設定します。
if (rotation == Surface.ROTATION_0) {
int degreesRotate = 90;
}
以下のスニペットを使用して、ビットマップのサイズを変更し、向きに基づいてビットマップを回転させます。
private Bitmap createBitmap(byte[] imageData, int maxWidth, int maxHeight,
int rotationDegrees) throws FileNotFoundException {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inDensity = 240;
int imageWidth = 0;
int imageHeight = 0;
Bitmap image = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options);
imageWidth = image.getWidth();
imageHeight = image.getHeight();
if (imageWidth > maxWidth || imageHeight > maxHeight) {
double imageAspect = (double) imageWidth / imageHeight;
double desiredAspect = (double) maxWidth / maxHeight;
double scaleFactor;
if (imageAspect < desiredAspect) {
scaleFactor = (double) maxHeight / imageHeight;
} else {
scaleFactor = (double) maxWidth / imageWidth;
}
float scaleWidth = ((float) scaleFactor) * imageWidth;
float scaleHeight = ((float) scaleFactor) * imageHeight;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(image,
(int) scaleWidth, (int) scaleHeight, true);
image = scaledBitmap;
}
if (rotationDegrees != 0) {
int w = image.getWidth();
int h = image.getHeight();
mtx.postRotate(rotationDegrees);
Bitmap rotatedBMP = Bitmap.createBitmap(image, 0, 0, w, h, mtx,
true);
image = rotatedBMP;
}
return image;
}
上記のメソッドは、方向に基づいてビットマップを返します。