0

I am trying to set the camera preview size and also the image size in the Camera Preview class. But, my Log.v to show the camera preview size and image size does not display anything in the logcat. So, I have no idea whether the camera preview and image is set to the size I wanted. I am using Eclipse.

I am calling the setCamera() from the Camera Preview constructor.

 public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;
    setCamera(mCamera);
    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

我设置的相机有Log.v来显示预览的大小和图像大小。

 public void setCamera(Camera camera) {
    if (mCamera == camera) { return; }

    mCamera = camera;

    if (mCamera != null) {
        List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();
        mSupportedPreviewSizes = localSizes;
        //parameters.setPreviewSize(mSupportedPreviewSizes.get(0).width, mSupportedPreviewSizes.get(0).height);
        parameters.setPreviewSize(640, 480);
        mCamera.setParameters(parameters);
        parameters.setPictureSize(640, 480);
        mCamera.setParameters(parameters);
        Log.v(preview, "get preview size: " + parameters.getPreviewSize() 
                + "get picture size: " +parameters.getPictureSize());
        requestLayout();


        try {
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            e.printStackTrace();
        }

        /*
          Important: Call startPreview() to start updating the preview surface. Preview must 
          be started before you can take a picture.
          */
        mCamera.startPreview();
    }
}
4

2 回答 2

0

您正在尝试打印 a Camera.Size, 使用parameters.getPreviewSize().widthandparameters.getPreviewSize().height代替。对于getPictureSize().

可能由于日志级别或标签过滤(preview变量的内容是什么?)而导致日志未显示,请尝试更改它:

Log.d("CAMERA_PREVIEW", "get preview size: " 
    + parameters.getPreviewSize().height + " " 
    + parameters.getPreviewSize().width 
    + "get picture size: " 
    + parameters.getPictureSize().height + " "
    + parameters.getPictureSize().width
);

甚至Log.e只是为了测试(这会产生误导,因为它不是错误,但只是暂时的,稍后再改回来)并查看日志是否出现(查找“CAMERA_PREVIEW”标签)。

于 2013-11-10T07:52:04.320 回答
0

试试这个它正在工作。

 public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = "Camera Preview";
    private static final double PREVIEW_SIZE_FACTOR = 3.00;
    private SurfaceHolder mHolder = null;
    private Camera mCamera = null;
    Camera.Parameters _parameters=null;

    @SuppressWarnings("deprecation")
    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            Camera.Parameters parameters = mCamera.getParameters();
            final Size mPreviewSize = getOptimalSize();
            parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
             requestLayout();
            mCamera.setParameters(parameters);
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
            //startContinuousAutoFocus();
        } catch (Exception e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }
    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    @SuppressLint("NewApi")
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here
        // set Camera parameters

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
        }
    public void startPreview(){
        try{
            mCamera.startPreview();
        }catch (Exception e) {
        }
            } 
            private Size getOptimalSize() {
        Camera.Size result = null;
        final Camera.Parameters parameters = mCamera.getParameters();
        Log.i(CameraPreview.class.getSimpleName(), "window width: " + getWidth() + ", height: " + getHeight());
        for (final Camera.Size size : parameters.getSupportedPreviewSizes()) {
            if (size.width <= getWidth() * PREVIEW_SIZE_FACTOR && size.height <= getHeight() * PREVIEW_SIZE_FACTOR) {
                if (result == null) {
                    result = size;
                } else {
                    final int resultArea = result.width * result.height;
                    final int newArea = size.width * size.height;

                    if (newArea > resultArea) {
                        result = size;
                    }
                }
            }
        }
        if (result == null) {
            result = parameters.getSupportedPreviewSizes().get(0);
        }
        Log.i(CameraPreview.class.getSimpleName(), "Using PreviewSize: " + result.width + " x " + result.height);
        return result;
    }
    }
于 2013-11-14T11:37:25.053 回答