Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I was changing the data structure received by the camera and Mat result and operations.

@Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

        Camera.Parameters parameters = mCamera.getParameters();

        Camera.Size size = parameters.getPreviewSize();
        previewSizeHeight = size.height;
        previewSizeWidth = size.width;

        // Set the camera preview size
        parameters.setPreviewSize(previewSizeWidth, previewSizeHeight);

        imageFormat = parameters.getPreviewFormat();

        mCamera.setParameters(parameters);

        mCamera.startPreview();

        mCamera.setPreviewCallback(new Camera.PreviewCallback() {

            public void onPreviewFrame(final byte[] data, final Camera camera) {

                synchronized (this) {
                    totalFrames++;

                    if (imageFormat == ImageFormat.NV21) {

                        //We only accept the NV21(YUV420) format.
                        if (!bProcessing) {

                            if(matFrameCamera != null) {
                                matFrameCamera.release();
                            }

                            matFrameCamera = new Mat(previewSizeHeight + (previewSizeHeight / 2), previewSizeWidth, CvType.CV_8UC1);

                            totalProcFrames++;

                            matFrameCamera.put(0, 0, data);
                            mRgba2Gray = new Mat();
                            Imgproc.cvtColor(matFrameCamera, mRgba2Gray, Imgproc.COLOR_YUV2RGBA_NV21, 4);
                            mHandler.post(doImageProcessing);

                        }
                    }

                    this.notify();
                }

            }
        });
    }

    private Runnable doImageProcessing = new Runnable() {
        public void run() {

            if (!stop) {
                bProcessing = true;

                Imgproc.resize(mRgba2Gray, mRgba2Gray, new Size(mRgba2Gray.cols() / 3, mRgba2Gray.rows() / 3));
                Bitmap bitmapCameraPortrait = Bitmap.createBitmap(mRgba2Gray.cols(), mRgba2Gray.rows(),
                        Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(mRgba2Gray, bitmapCameraPortrait);
                bitmap = scaleDown(bitmapCameraPortrait, mRgba2Gray.height() / 3, true);
                Utils.bitmapToMat(bitmapCameraPortrait, mRgba2Gray);
                Imgproc.cvtColor(mRgba2Gray, mRgba2Gray, Imgproc.COLOR_BGRA2GRAY);

                Mat matImageMatcher = new Mat();

                SurfMatcher(mRgba2Gray.getNativeObjAddr(), matTarget.getNativeObjAddr(),
                        matImageMatcher.getNativeObjAddr());

                int newWidthBitmap = bitmapCameraPortrait.getWidth() +
                        (matImageMatcher.cols() - bitmapCameraPortrait.getWidth());
                int newHeightBitmap = bitmapCameraPortrait.getHeight() +
                        (matImageMatcher.rows() - bitmapCameraPortrait.getHeight());

                bitmapCameraPortrait = Bitmap.createScaledBitmap(bitmapCameraPortrait, newWidthBitmap,
                        newHeightBitmap, true);

                Utils.matToBitmap(matImageMatcher, bitmapCameraPortrait);

                bitmap = bitmapCameraPortrait;

                ((Activity) context).runOnUiThread(new TimerTask() {
                    @Override
                    public void run() {
                        ivCameraPreview.setImageBitmap(bitmap);
                    }
                });

                stop = true;

                bProcessing = false;

            }

        }

    };