1
votes

I am currently making my android app with camera2 API. 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.

First of all, I only take pictures in landscape mode.

My textureView is exactly the same size like my display. 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)

But look (only looking in portrait at it, but there is no orientation change, always horizontal):

Looking in landscape

Looking in landscape

1
Nobody can help? I found lots of post, but nothing is helping - Damaro

1 Answers

0
votes

Have you looked at the AutoFitTextureView in the sample camera2 apps from Google?

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.