0

我是 Android 编程新手。我在安卓手机上做了一个项目。它适用于我的 HTC 渴望 a8181。因为我希望我的软件运行得更快,所以我得到了一个 htc one 。然后程序无法从相机获取帧。以前有人遇到过这样的问题吗?请帮我。(我使用 onPreviewFrame 在我的软件中获取框架。)

public class CopyOfCameraPreview extends SurfaceView implements
    SurfaceHolder.Callback, PreviewCallback {
private SurfaceHolder mHolder;
private Camera mCamera;
private boolean preview = false;
public static int w, h, l;

public CopyOfCameraPreview1(Context context) {
    super(context);
    mCamera = getCameraInstance();  
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}



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


    if (mHolder.getSurface() == null) {

        return;
    }

    try {
        mCamera.stopPreview();
        preview = false;
    } catch (Exception e) {

    }

    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
        preview = false;

    } catch (Exception e) {
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
}


public void surfaceCreated(SurfaceHolder holder) {

    try {
        mCamera.setPreviewDisplay(holder);
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(960, 544);
        parameters.setPreviewFormat(PixelFormat.YCbCr_420_SP);

        mCamera.setParameters(parameters);
        mCamera.setPreviewCallback(this);

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

}


public void surfaceDestroyed(SurfaceHolder holder) {
    if (mCamera != null) {

        if (preview) {
            mCamera.stopPreview();
            preview = false;

        }
        mCamera.release();
        mCamera = null;

    }

}

public static Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e) {

    }
    return c; 
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    camera.setPreviewCallback(null);
    if (data != null) {
        Camera.Parameters parameters = mCamera.getParameters();
        int imageFormat = parameters.getPreviewFormat();
        if (imageFormat == ImageFormat.NV21) {
            // Bitmap image = null;
            w = parameters.getPreviewSize().width;
            h = parameters.getPreviewSize().height;
            l = data.length;
            decodeYUV420SP(data, w, h);
        }

    }

    camera.setPreviewCallback(this);
}

}

06-22 17:41:51.159: I/QCameraHWI(191):  setPreviewWindow : X, mPreviewState = 0
06-22 17:41:51.159: I/CameraService(191): Destroying camera 0
06-22 17:41:51.159: I/QualcommCamera(191): Qint android::close_camera_device(hw_device_t*): device =0x14e1270 E
06-22 17:41:51.159: I/QCameraHWI(191): ~QCameraHardwareInterface: E
06-22 17:41:51.169: I/drawView(16359): Drwaed
06-22 17:41:51.169: E/mm_camera_poll_thread(191): mm_camera_poll_thread_release: err, poll thread of channel  0s not running. cam_id=0
06-22 17:41:51.169: E/mm_camera_poll_thread(191): mm_camera_poll_thread_release: err,   poll thread of channel  1s not running. cam_id=0
06-22 17:41:51.169: E/mm_camera_poll_thread(191): mm_camera_poll_thread_release: err, poll thread of channel  2s not running. cam_id=0
06-22 17:41:51.169: E/mm_camera_poll_thread(191): mm_camera_poll_thread_release: err, poll thread of channel  3s not running. cam_id=0
06-22 17:41:51.169: E/mm-camera(206): config_shutdown_pp Camera not in streaming mode. Returning. 
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_read_pipe Got EXIT 
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_clear_buf: E
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_clear_buf: Check for image mode 0 num 0 count = 0 data = 0x0
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_clear_buf: Check for image mode 1 num -1 count = -1 data = 0x0
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_clear_buf: Check for image mode 2 num 0 count = 0 data = 0x0
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_clear_buf: Check for image mode 3 num 0 count = 0 data = 0x0
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_clear_buf: Check for image mode 4 num 0 count = 0 data = 0x0
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_clear_buf: Check for image mode 5 num 0 count = 0 data = 0x0
06-22 17:41:51.169: E/mm-camera(206): mctl_pp_clear_buf: X
4

1 回答 1

0

相机预览管理有点棘手 - 只有在创建并调用了 surfaceChanged() 后,表面视图才可用于相机预览。您还应记住在活动 onPause() / onResume() 生命周期中停止预览和释放相机。这就是为什么最好将相机管理与活动而不是查看课程结合起来。

Nd 无需在 onPreviewFrame() 回调中查询相机参数 - 它们不会改变,因为您的应用程序仅具有相机。

您可以查看此项目中的示例,了解它应该如何工作:

http://sourceforge.net/projects/javaocr/

(查看源代码并查看 demos 子目录 - 除非您对图像处理内容感兴趣)

于 2012-06-23T09:05:41.690 回答