camera not opening while using opencv

asked 2013-06-04 05:13:31 -0500

Ankit gravatar image

I am processing byte from onPreviewFrame() to create a bitmap and finally draw the bitmap using canvas. I'm attatching my code. Camera is showing black screen.

CameraDemo Class public class CameraDemo extends Activity {

public static final String TAG = "Camera-Activity";
private Preview preview;
private FrameLayout framelayout;
private int width = 1024;
private int height = 720;

//BaseLoaderCallBack
 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    System.loadLibrary("native_activity");
                    preview.mCamera = Camera.open();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };


@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     //Set this APK Full screen
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
      //Set this APK no title
      requestWindowFeature(Window.FEATURE_NO_TITLE);  
      setContentView(R.layout.main);

      Log.i(TAG, "Trying to load OpenCV library");
        if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this,
                mLoaderCallback)) {
            Log.e(TAG, "Cannot connect to OpenCV Manager");
        }
        SurfaceView camView = new SurfaceView(this);
        SurfaceHolder camHolder = camView.getHolder();
        preview = new Preview(width,height);

        camHolder.addCallback(preview);

        framelayout = (FrameLayout) findViewById(R.id.frameLayout);
        framelayout.addView(camView, new LayoutParams(width, height));
}

}

PreviewClass

epublic class Preview implements SurfaceHolder.Callback,Camera.PreviewCallback

{//Class Starts public Camera mCamera; //Camera Object private int PreviewSizeWidth; //Width of preview size private int PreviewSizeHeight; //Height of Preview Size private static final String TAG = "Preview"; private SurfaceHolder mHolder;

//Constructor to set preview size width and height
public Preview(int width, int height)
{
    PreviewSizeWidth = width;
    PreviewSizeHeight = height;
    Log.i(TAG, "Instantiated new Preview");
}

//Override onpreviewFrame method of camera.previewcallback
@Override
public void onPreviewFrame(byte[] arg0, Camera arg1)
{
    Log.i(TAG, "Frame is Received");
    Bitmap bmp = processFrame(arg0);

    if (bmp != null) {
        draw(bmp);
    }
    bmp.recycle();
}

//Called when surface is changed
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 
{
    Parameters parameters;

    parameters = mCamera.getParameters();
    // Set the camera preview size
    parameters.setPreviewSize(PreviewSizeWidth, PreviewSizeHeight);
    // Set the take picture size, you can set the large size of the camera supported
    mCamera.setParameters(parameters);
    mCamera.startPreview();
}

//Called when surface is created
@Override
public void surfaceCreated(SurfaceHolder arg0) {
    mCamera = Camera.open();
    try
    {
        // If did not set the SurfaceHolder, the preview area will be black.
        mCamera.setPreviewDisplay(arg0);
        mCamera.setPreviewCallback(this);
    } 
    catch (IOException e)
    {
        mCamera.release();
        mCamera = null;
    }

}

//Called when surface is destroyed
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    mCamera.setPreviewCallback(null);
    mCamera.stopPreview();
    mCamera.release();
    mCamera = null;

}

//Process Frame Data
protected Bitmap processFrame(byte[] data)
{
    Log.i(TAG, "Frame is Processing");
    Mat mYUV = new Mat(PreviewSizeHeight + PreviewSizeHeight/2, PreviewSizeWidth, CvType.CV_8UC1);
    mYUV.put(0, 0, data);
    Mat mRgba = new Mat();
    Imgproc.cvtColor(mYUV, mRgba, Imgproc.COLOR_YUV2RGB_NV21, 3);
    Bitmap mBitmap = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
     try {
         Log.i(TAG, "Bitmap1");
            Utils.matToBitmap(mRgba, mBitmap);
        } catch(Exception e) {
            Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
            mBitmap.recycle();
            mBitmap = null;
        }
    return mBitmap;
}

//For drawing bitmap
public void draw(Bitmap bmp){
    Log.i(TAG, "Drawing Canvas");
    Canvas canvas = mHolder.lockCanvas();
    if (canvas != null) {
        canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
       // mFps.draw(canvas, (canvas ...
(more)
edit retag flag offensive close merge delete