Android:在横向模式下捕获的照片强制

Doo*_*ght 1 camera android photo image

嗨已建立在Camera Preview上.

原始它甚至在纵向模式下也显示了风景预览.我用一些额外的代码更改了这个.但是,无论您的风景/肖像如何,它始终以横向模式保存图像.

我现在也强迫它进入肖像模式:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)

基本上我拍的任何图像都是在风景中出现的.(旋转肖像拍摄图像).

我如何使它在定向模式下保存照片,或锁定到肖像假设?

我考虑过将照片阵列旋转90度.但这需要将图像绘制到位图,旋转然后存储回数组.矫枉过正吗?除非您可以直接旋转图像阵列?

Ven*_*nky 7

首先使用以下代码段检查相机方向:

    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;
   }
Run Code Online (Sandbox Code Playgroud)

然后使用并设置您的方向检查您想要的旋转:

if (rotation == Surface.ROTATION_0) {
    int degreesRotate = 90;
}
Run Code Online (Sandbox Code Playgroud)

使用以下代码段调整位图大小并根据方向旋转位图:

    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;
}
Run Code Online (Sandbox Code Playgroud)

上述方法将根据Orientation返回位图.

  • +1的答案,包含OP所需的一切.-1为OP甚至不说"谢谢"......或者接受它作为正确的答案. (2认同)