Code example for Camera

Methods: releasesetPreviewCallbacksetPreviewDisplaystartPreview

-1
        // preview callbacks in the queue after quit is called. The preview 
        // callback still uses the camera object (setHasPreviewCallback). 
        // After camera is released, RuntimeException will be thrown from 
        // the method. So we need to join the looper thread here. 
        mLooper.getThread().join();
        mCamera.release();
    } 
     
    //Implement the previewCallback 
    private final class RawPreviewCallback implements PreviewCallback { 
        public void onPreviewFrame(byte [] rawData, Camera camera) {         
            Log.v(TAG, "Preview callback start");            
            int rawDataLength = 0;
            if (rawData != null) {
                rawDataLength = rawData.length;
            } 
            if (rawDataLength > 0) {
                rawPreviewCallbackResult = true;
            } else { 
                rawPreviewCallbackResult = false;
            } 
            mPreviewDone.open();
            Log.v(TAG, "Preview callback stop");
        } 
    }; 
     
    //Implement the shutterCallback 
    private final class TestShutterCallback implements ShutterCallback {
        public void onShutter() { 
            shutterCallbackResult = true;
            Log.v(TAG, "onShutter called");
        } 
    }; 
     
    //Implement the RawPictureCallback 
    private final class RawPictureCallback implements PictureCallback { 
        public void onPictureTaken(byte [] rawData, Camera camera) {
            // no support for raw data - success if we get the callback 
            rawPictureCallbackResult = true;
            Log.v(TAG, "RawPictureCallback callback");
        } 
    }; 
     
    //Implement the JpegPictureCallback 
    private final class JpegPictureCallback implements PictureCallback {   
        public void onPictureTaken(byte [] rawData, Camera camera) {           
            try {          
                if (rawData != null) {
                    int rawDataLength = rawData.length;
                    File rawoutput = new File(
                            Environment.getExternalStorageDirectory().toString(), "/test.bmp");
                    FileOutputStream outstream = new FileOutputStream(rawoutput);
                    outstream.write(rawData);                   
                    Log.v(TAG, "JpegPictureCallback rawDataLength = " + rawDataLength);
                    jpegPictureCallbackResult = true;
                } else { 
                    jpegPictureCallbackResult = false;
                } 
                mSnapshotDone.open();
                Log.v(TAG, "Jpeg Picture callback");
            } catch (Exception e) {
                Log.v(TAG, e.toString());
            } 
        } 
    }; 
    
    private void waitForPreviewDone() { 
        if (!mPreviewDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
            Log.v(TAG, "waitForPreviewDone: timeout");
        } 
        mPreviewDone.close();
    } 
 
    private void waitForSnapshotDone() { 
        if (!mSnapshotDone.block(MediaNames.WAIT_SNAPSHOT_TIME)) {
            // timeout could be expected or unexpected. The caller will decide. 
            Log.v(TAG, "waitForSnapshotDone: timeout");
        } 
        mSnapshotDone.close();
    } 
 
 
    private void checkTakePicture() {  
        SurfaceHolder mSurfaceHolder;
        try { 
            mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
            mCamera.setPreviewDisplay(mSurfaceHolder);
            Log.v(TAG, "Start preview");
            mCamera.startPreview();
            waitForPreviewDone(); 
            mCamera.setPreviewCallback(null);
            mCamera.takePicture(mShutterCallback, mRawPictureCallback, mJpegPictureCallback);
            waitForSnapshotDone(); 
        } catch (Exception e) {
            Log.v(TAG, e.toString());
        }       
    } 
     
    private void checkPreviewCallback() {  
        SurfaceHolder mSurfaceHolder;
        try { 
            mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
            mCamera.setPreviewDisplay(mSurfaceHolder);
            Log.v(TAG, "start preview");
            mCamera.startPreview();
            waitForPreviewDone(); 
            mCamera.setPreviewCallback(null);
        } catch (Exception e) {
            Log.v(TAG, e.toString());
        }       
    } 
     
    /* 
     * TODO(yslau): Need to setup the golden rawData and compare the  
     * the new captured rawData with the golden one.        
     *  
     * Test case 1: Take a picture and verify all the callback 
     * functions are called properly. 
     */ 
    @LargeTest 
    public void testTakePicture() throws Exception {   
        initializeMessageLooper(); 
        mCamera.setPreviewCallback(mRawPreviewCallback);
        checkTakePicture(); 
        terminateMessageLooper(); 
        assertTrue("shutterCallbackResult", shutterCallbackResult);
        assertTrue("rawPictureCallbackResult", rawPictureCallbackResult);
        assertTrue("jpegPictureCallbackResult", jpegPictureCallbackResult);
    } 
     
    /* 
     * Test case 2: Set the preview and  
     * verify the RawPreviewCallback is called 
     */ 
    @LargeTest 
    public void testCheckPreview() throws Exception {   
        initializeMessageLooper(); 
        mCamera.setPreviewCallback(mRawPreviewCallback);
        checkPreviewCallback();      
        terminateMessageLooper(); 
        assertTrue("RawPreviewCallbackResult", rawPreviewCallbackResult);
    }