Android 4.4 - 从不调用Camera.PreviewCallback

时间:2015-09-16 01:42:33

标签: android camera android-4.4-kitkat

我经历了http://developer.android.com/guide/topics/media/camera.html#custom-camera 我试图添加一个Camera.PreviewCallback,但它没有被调用。哪些方法/类依赖于PreviewCallback触发?

public class MainActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;
    private final static String TAG = "Main-Activity";

    private Camera.PreviewCallback countFrameCallback = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] bytes, Camera camera) {
            Log.d(TAG, "In frame callback");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
        mCamera.setPreviewCallback(countFrameCallback);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    /** Check if this device has a camera */
    private boolean checkCameraHardware(Context context) {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        } catch (Exception e) {
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }
}

和CameraPreview:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder mHolder;
    private Camera mCamera;
    private final static String TAG = "Camera-Preview";


    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 {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();

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

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    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

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

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

1 个答案:

答案 0 :(得分:3)

相机回调有点特别。来自Camera Developer reference

  

大多数长时间运行的操作(预览,聚焦,照片捕捉等)   异步发生并根据需要调用回调。回调   将在事件线程上调用open(int)从。

调用

要获得回调,并且因为您从主活动中获取了摄像头,只需实现PreviewCallback并覆盖其方法,如下所示:

mPreview = new CameraPreview(this, mCamera, this); //This stand for the PreviewCallback

然后,当您启动CameraPreview类时,将MainActivity的回调作为参数传递,如下所示:

<强> MainActivity

mCamera.setPreviewCallback(countFrameCallback);

从MainActivity中删除private Camera.PreviewCallback mCallback; ... //Constructor public CameraPreview(Context context, Camera camera, Camera.PreviewCallback callback) { super(context); mCallback = callback; ... }

<强> CameraPreview

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

在SurfaceChanged方法中:

CREATE OR REPLACE PROCEDURE job1
AS
  vblSQL  VARCHAR(32767);
  vJobID  NUMBER:=1;

BEGIN
  EXECUTE IMMEDIATE'insert into job_logs values('''||vJobID||''',sysdate,NULL)';
  vblSQL:= 'UPDATE jobs SET status=''R'' WHERE job_ID=1';
  EXECUTE IMMEDIATE(vblSQL);
  Dbms_Lock.sleep(45);
  vblSQL:= 'UPDATE jobs SET status=''Y'' WHERE job_ID=1';
  EXECUTE IMMEDIATE(vblSQL);
  vblSQL:= 'UPDATE job_logs SET endtime=SYSDATE WHERE jobID=1';
  EXECUTE IMMEDIATE(vblSQL);
END;