Example usage for android.media ImageReader getSurface

List of usage examples for android.media ImageReader getSurface

Introduction

In this page you can find the example usage for android.media ImageReader getSurface.

Prototype

public Surface getSurface() 

Source Link

Document

Get a Surface that can be used to produce Image Images for this ImageReader .

Until valid image data is rendered into this Surface , the #acquireNextImage method will return null .

Usage

From source file:MainActivity.java

protected void takePicture(View view) {
    if (null == mCameraDevice) {
        return;//from  w  w  w. java2 s. com
    }
    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraDevice.getId());
        StreamConfigurationMap configurationMap = characteristics
                .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        if (configurationMap == null)
            return;
        Size largest = Collections.max(Arrays.asList(configurationMap.getOutputSizes(ImageFormat.JPEG)),
                new CompareSizesByArea());
        ImageReader reader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG,
                1);
        List<Surface> outputSurfaces = new ArrayList<Surface>(2);
        outputSurfaces.add(reader.getSurface());
        outputSurfaces.add(new Surface(mTextureView.getSurfaceTexture()));
        final CaptureRequest.Builder captureBuilder = mCameraDevice
                .createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(reader.getSurface());
        captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
        ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                Image image = null;
                try {
                    image = reader.acquireLatestImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.capacity()];
                    buffer.get(bytes);
                    OutputStream output = new FileOutputStream(getPictureFile());
                    output.write(bytes);
                    output.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (image != null) {
                        image.close();
                    }
                }
            }
        };
        HandlerThread thread = new HandlerThread("CameraPicture");
        thread.start();
        final Handler backgroudHandler = new Handler(thread.getLooper());
        reader.setOnImageAvailableListener(readerListener, backgroudHandler);
        final CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
            @Override
            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                    TotalCaptureResult result) {
                super.onCaptureCompleted(session, request, result);
                Toast.makeText(MainActivity.this, "Picture Saved", Toast.LENGTH_SHORT).show();
                startPreview(session);
            }
        };
        mCameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(CameraCaptureSession session) {
                try {
                    session.capture(captureBuilder.build(), captureCallback, backgroudHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConfigureFailed(CameraCaptureSession session) {
            }
        }, backgroudHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.sien.cpshoot.screencapture.ScreenCaptureFragment.java

private void setUpVirtualDisplay() {
    Log.i(TAG, "Setting up a VirtualDisplay: " + mSurfaceView.getWidth() + "x" + mSurfaceView.getHeight() + " ("
            + mScreenDensity + ")");

    //---------------cp.add
    final int w = mSurfaceView.getWidth();
    final int h = mSurfaceView.getHeight();
    final ImageReader mImageReader = ImageReader.newInstance(w, h, ImageFormat.RGB_565, 2);
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager mWindowManager = getActivity().getWindowManager();
    mWindowManager.getDefaultDisplay().getMetrics(metrics);
    int mScreenDensity = metrics.densityDpi;
    //----end//  w  w  w . j ava2  s.c  o m
    mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture", mSurfaceView.getWidth(),
            mSurfaceView.getHeight(), mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
            mImageReader.getSurface(), null, null);

    //---------------cp.add
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            saveImage(mImageReader, w, h);
        }
    }, 100);

    //-----end
    mButtonToggle.setText(R.string.stop);
}