简体   繁体   English

Android Java:将Camera2预览图像拉伸

[英]Android Java: Camera2 preview image is stretched

I am currently making my android app with camera2 API. 我目前正在使用camera2 API开发我的android应用。 I am able to make images, save them, edit them and so on. 我能够制作图像,保存图像,编辑图像等等。 But my camera is always stretched. 但是我的相机总是拉长的。

I am using the Samsung Galaxy Tab A and the Samsung Galaxy Note 8, so not 16:9 ratio. 我正在使用Samsung Galaxy Tab A和Samsung Galaxy Note 8,而不是16:9的比例。

First of all, I only take pictures in landscape mode. 首先,我只在横向模式下拍照。

My textureView is exactly the same size like my display. 我的textureView与我的显示器大小完全相同。 My preview should be better, because I use its size for saving the image also. 我的预览效果会更好,因为我也使用它的大小来保存图像。 But also if setting it to the same size like my display I get stretched images. 但是,如果将其设置为与我的显示器相同的尺寸,则会得到拉伸图像。

            //getting the ratio, correct setting of sizes
        if (verh == sechzehnzuneun) {
            newSize = new Size(2300, 1350);
        } else if (verh == sechzehnzuzehn) {
            newSize = new Size(2400, 1500);
        } else if (verh == note8) {
            newSize = new Size(2792, 1440);
        }
//also used for saving
        _imageDimension = newSize;

        //used for rotating the preview, otherwise i geht some strange portrait camera 
Matrix matrix = new Matrix();
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        RectF textureRectF = new RectF(0, 0, _textureView.getWidth(), _textureView.getHeight());
        RectF previewRectF = new RectF(0, 0, _imageDimension.getHeight(), _imageDimension.getWidth());
        float centerX = textureRectF.centerX();
        float centerY = textureRectF.centerY();
        if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
            previewRectF.offset(centerX - previewRectF.centerX(),
                    centerY - previewRectF.centerY());
            matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL);
            float scale = Math.max((float) width / _imageDimension.getWidth(),
                    (float) height / _imageDimension.getHeight());
            matrix.postScale(scale, scale, centerX, centerY);
            matrix.postRotate(90 * (rotation - 2), centerX, centerY);
        } else {
            //neu weg wenn nicht
            previewRectF.offset(centerX - previewRectF.centerX(), centerY - previewRectF.centerY());
            matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL);
            float scale = Math.max((float)height / _imageDimension.getHeight(), (float)width / _imageDimension.getWidth());
            matrix.postScale(scale, scale,centerX,centerY);
            matrix.postRotate(0, centerX, centerY);
        }
        _textureView.setTransform(matrix);

I am getting the if case of upper code. 我正在上代码的情况下。

But look (only looking in portrait at it, but there is no orientation change, always horizontal) 但是请看(仅以纵向看,但方向没有变化,始终是水平的)

但是,请看(仅纵向查看,但方向不变,始终水平):

Looking in landscape 看风景

看风景

Have you looked at the AutoFitTextureView in the sample camera2 apps from Google? 您是否在Google的示例camera2应用程序中查看了AutoFitTextureView

There's no particular guarantee that the camera device supports exactly the display size of the device, and if it doesn't, a size close to the requested size is selected. 没有特别保证相机设备完全支持设备的显示尺寸,如果不支持,则选择接近要求尺寸的尺寸。 But that will likely not have the same aspect ratio. 但这可能不会具有相同的纵横比。

You need to pick a size that the camera device actually supports, and then set your TextureView to a matching aspect ratio. 您需要选择相机设备实际支持的尺寸,然后将TextureView设置为匹配的宽高比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM