Yudi karma
Yudi karma

Reputation: 324

camera2 capture image different with preview

my capture picture output not same with my camera preview in landscape mode

  1. before cpture before capture

  2. after capture

after capture

whats wrong ? and whats have i do. thanks

Upvotes: 0

Views: 1257

Answers (1)

phatnhse
phatnhse

Reputation: 3920

This is the AutoFitTextView class which I pulled from Google sample. You can take a look at here. It aims to show camera view and config the ratio base on the physical size of device.

public class AutoFitTextureView extends TextureView {

    private int mRatioWidth = 0;
    private int mRatioHeight = 0;

    // Some codes here...

    public void setAspectRatio(int width, int height) {
        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("Size cannot be negative.");
        }
        mRatioWidth = width;
        mRatioHeight = height;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (0 == mRatioWidth || 0 == mRatioHeight) {
            setMeasuredDimension(width, height);
        } else {
            if (width < height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
            }
        }
    }
}

There are 2 points in this class:

  1. You can't ensure the ratio works properly in every device. However, we are able to choose optimized size which is already defined in this class.
  2. This condition is wrong: if (width < height * mRatioWidth / mRatioHeight). It should be > because when width is bigger than height, we calculate and set measure dimension base on width (not height).

UPDATED

If you just want every device will work properly in a particular ratio, then set hard ratio for it (for instance: 4/3)

You can achieve that by replacing those lines of code:

mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                        rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                        maxPreviewHeight, largest);

-> previewSize = Size(4, 3)

Upvotes: 2

Related Questions