Example usage for android.graphics YuvImage YuvImage

List of usage examples for android.graphics YuvImage YuvImage

Introduction

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

Prototype

public YuvImage(byte[] yuv, int format, int width, int height, int[] strides) 

Source Link

Document

Construct an YuvImage.

Usage

From source file:org.deviceconnect.android.deviceplugin.host.camera.CameraOverlay.java

@Override
public void onPreviewFrame(final byte[] data, final Camera camera) {
    synchronized (mCameraLock) {
        final long currentTime = System.currentTimeMillis();
        if (mLastFrameTime != 0) {
            if ((currentTime - mLastFrameTime) < mFrameInterval) {
                mLastFrameTime = currentTime;
                return;
            }/*from  ww  w .  j ava2  s  . c  om*/
        }

        if (mCamera != null && mCamera.equals(camera)) {
            mCamera.setPreviewCallback(null);

            if (mServer != null) {
                int format = mPreview.getPreviewFormat();
                int width = mPreview.getPreviewWidth();
                int height = mPreview.getPreviewHeight();

                YuvImage yuvimage = new YuvImage(data, format, width, height, null);
                Rect rect = new Rect(0, 0, width, height);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                if (yuvimage.compressToJpeg(rect, JPEG_COMPRESS_QUALITY, baos)) {
                    byte[] jdata = baos.toByteArray();

                    int degree = mPreview.getCameraDisplayOrientation(mContext);
                    if (degree == 0) {
                        mServer.offerMedia(jdata);
                    } else {
                        BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
                        bitmapFactoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
                        Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length,
                                bitmapFactoryOptions);
                        if (bmp != null) {
                            Matrix m = new Matrix();
                            m.setRotate(degree * mFacingDirection);

                            Bitmap rotatedBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
                                    m, true);
                            if (rotatedBmp != null) {
                                baos.reset();
                                if (rotatedBmp.compress(CompressFormat.JPEG, JPEG_COMPRESS_QUALITY, baos)) {
                                    mServer.offerMedia(baos.toByteArray());
                                }
                                rotatedBmp.recycle();
                            }
                            bmp.recycle();
                        }
                    }
                }
            }

            mCamera.setPreviewCallback(this);
        }

        mLastFrameTime = currentTime;
    }
}