Example usage for android.graphics YuvImage compressToJpeg

List of usage examples for android.graphics YuvImage compressToJpeg

Introduction

In this page you can find the example usage for android.graphics YuvImage compressToJpeg.

Prototype

public boolean compressToJpeg(Rect rectangle, int quality, OutputStream stream) 

Source Link

Document

Compress a rectangle region in the YuvImage to a jpeg.

Usage

From source file:hr.abunicic.angular.CameraActivity.java

/**
 * Method that starts detection of shapes.
 *//*w  w  w .java 2s. c  o m*/
public void startDetection() {
    startPreview = !startPreview;

    mCamera.startPreview();
    mCamera.setPreviewCallback(new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {

            //Getting engine results
            VisionEngineResults res = JobScheduler.getEngineResults();
            if (res != null) {
                rp = res;

                updateDescription();
            }

            if (inScheduler < 3 && startPreview) {

                inScheduler++;

                cornersView.setVisibility(View.VISIBLE);

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                params = mCamera.getParameters();
                Camera.Size size = params.getPreviewSize();

                //Compressing frame image to JPEG and then to byte array
                YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
                Rect rectangle = new Rect();
                rectangle.bottom = size.height;
                rectangle.top = 0;
                rectangle.left = 0;
                rectangle.right = size.width;
                yuvImage.compressToJpeg(rectangle, 20, out);
                byte[] imageBytes = out.toByteArray();

                //Starting the PrepareTask for this frame
                new PrepareTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, imageBytes);
                /*
                                    Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes , 0, imageBytes.length);
                                    try {
                Matrix matrix = new Matrix();
                        
                matrix.postRotate(90);
                        
                Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,bitmap.getWidth(),bitmap.getHeight(),true);
                        
                Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
                //storeImage(rotatedBitmap);
                if (mRecognizer.getCurrentState().equals(Recognizer.State.READY)) {
                    mRecognizer.recognizeBitmap(rotatedBitmap, Orientation.ORIENTATION_LANDSCAPE_RIGHT, CameraActivity.this);
                }
                                    } catch (IllegalStateException e) {
                        
                                    }
                */

                if (croppedBitmap != null) {
                    if (mRecognizer.getCurrentState().equals(Recognizer.State.READY)) {
                        storeImage(croppedBitmap);
                        mRecognizer.recognizeBitmap(croppedBitmap, Orientation.ORIENTATION_LANDSCAPE_RIGHT,
                                CameraActivity.this);
                    }
                } else {

                }

                camera.startPreview();

            } else if (!startPreview) {
                cornersView.setVisibility(View.GONE);
            }

        }
    });
}