Android Studio上SurfaceView上的拉伸相机预览

时间:2015-08-28 18:14:58

标签: android camera surfaceview

这是我在使SurfaceView变小时得到的结果。如何解决这个问题,使其显示正确的宽高比?我不想使用从相机中获取的整个图像,只是其中的一部分。

将其想象为Facebook Messenger应用程序,当您想从应用程序拍摄即时照片时,会出现一个小型相机窗口。我想要相同的结果。这是我的代码:

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;
import java.util.List;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

    public static final String TAG = "AKSQS";
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        //Take care of rotation and resizing here.

        mCamera.stopPreview();
        mCamera.setDisplayOrientation(90);

        Camera.Parameters parameters = mCamera.getParameters();
        List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
        for (Camera.Size sizesl : sizes) {
            //This is just to see the supported preview sizes.
            Log.d(TAG, "Width: " + String.valueOf(sizesl.width));
            Log.d(TAG, "Height: " + String.valueOf(sizesl.height));
            Log.d(TAG, "--------");
        }
//        parameters.setPreviewSize(1080,288);

        mCamera.setParameters(parameters);

        mCamera.startPreview();

        if (mHolder.getSurface() == null) {
            return;
        }

        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            Log.d(TAG, "Tried to stop a non-existing preview. Message: " + e.getMessage());
        }

        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e) {
            Log.d(TAG, "Error starting camra preview: " + e.getMessage());
        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

}

这是我的xml文件:

<RelativeLayout ...>

<FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:id="@+id/camera_preview"
        android:layout_alignParentBottom="true">
    </FrameLayout>

</RelativeLayout>

以下是我得到的结果:

http://i.stack.imgur.com/AEx2C.jpg

1 个答案:

答案 0 :(得分:1)

这一直困扰着我一个月。我终于找到了答案:

https://stackoverflow.com/a/22758359/5273230

这是最终结果:

http://imgur.com/jkrHkSV