Log.v不显示用于设置相机预览和图像尺寸的显示输出

时间:2013-11-10 07:13:23

标签: android eclipse image logging android-camera

我正在尝试设置相机预览尺寸以及相机预览类中的图像尺寸。但是,我的Log.v显示相机预览大小和图像大小不会在logcat中显示任何内容。所以,我不知道相机预览和图像是否设置为我想要的尺寸。我正在使用Eclipse。

我从Camera Preview构造函数调用setCamera()。

 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();
    }
}

2 个答案:

答案 0 :(得分:0)

您正在尝试打印Camera.Size,而是使用parameters.getPreviewSize().widthparameters.getPreviewSize().heightgetPictureSize()也一样。

由于日志级别或标记已过滤(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”标记)。 / p>

答案 1 :(得分: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;
    }
    }