Search in sources :

Example 1 with ResultCallback

use of com.google.android.gms.common.api.ResultCallback in project android-wearcamera by dheera.

the class MainActivity method surfaceChanged.

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    if (mPreviewRunning) {
        mCamera.stopPreview();
    }
    if (mSurfaceHolder.getSurface() == null) {
        return;
    }
    Camera.Parameters p = mCamera.getParameters();
    List<String> focusModes = p.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
        p.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    }
    p.setFlashMode(currentFlashMode);
    mCamera.setParameters(p);
    try {
        if (mCamera != null) {
            mCamera.setPreviewDisplay(arg0);
            setCameraDisplayOrientation();
            mCamera.setPreviewCallback(new Camera.PreviewCallback() {

                public void onPreviewFrame(byte[] data, Camera arg1) {
                    if (mWearableNode != null && readyToProcessImage && mPreviewRunning && displayFrameLag < 6 && displayTimeLag < 2000 && System.currentTimeMillis() - lastMessageTime < 4000) {
                        readyToProcessImage = false;
                        Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
                        int[] rgb = decodeYUV420SP(data, previewSize.width, previewSize.height);
                        Bitmap bmp = Bitmap.createBitmap(rgb, previewSize.width, previewSize.height, Bitmap.Config.ARGB_8888);
                        int smallWidth, smallHeight;
                        int dimension = 200;
                        // stream is lagging, cut resolution and catch up
                        if (displayTimeLag > 1500) {
                            dimension = 50;
                        } else if (displayTimeLag > 500) {
                            dimension = 100;
                        } else {
                            dimension = 200;
                        }
                        if (previewSize.width > previewSize.height) {
                            smallWidth = dimension;
                            smallHeight = dimension * previewSize.height / previewSize.width;
                        } else {
                            smallHeight = dimension;
                            smallWidth = dimension * previewSize.width / previewSize.height;
                        }
                        Matrix matrix = new Matrix();
                        matrix.postRotate(mCameraOrientation);
                        Bitmap bmpSmall = Bitmap.createScaledBitmap(bmp, smallWidth, smallHeight, false);
                        Bitmap bmpSmallRotated = Bitmap.createBitmap(bmpSmall, 0, 0, smallWidth, smallHeight, matrix, false);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bmpSmallRotated.compress(Bitmap.CompressFormat.WEBP, 30, baos);
                        displayFrameLag++;
                        sendToWearable(String.format("show %d", System.currentTimeMillis()), baos.toByteArray(), new ResultCallback<MessageApi.SendMessageResult>() {

                            @Override
                            public void onResult(MessageApi.SendMessageResult result) {
                                if (displayFrameLag > 0)
                                    displayFrameLag--;
                            }
                        });
                        bmp.recycle();
                        bmpSmall.recycle();
                        bmpSmallRotated.recycle();
                        readyToProcessImage = true;
                    }
                }
            });
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    mCamera.startPreview();
    mPreviewRunning = true;
}
Also used : ResultCallback(com.google.android.gms.common.api.ResultCallback) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) MessageApi(com.google.android.gms.wearable.MessageApi) Camera(android.hardware.Camera)

Aggregations

Bitmap (android.graphics.Bitmap)1 Matrix (android.graphics.Matrix)1 Camera (android.hardware.Camera)1 ResultCallback (com.google.android.gms.common.api.ResultCallback)1 MessageApi (com.google.android.gms.wearable.MessageApi)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1