How to get single preview frame in Camera2 API Android 5.0? How to get single preview frame in Camera2 API Android 5.0? android android

How to get single preview frame in Camera2 API Android 5.0?


A little late but better than never:

Usually a TextureView is used to display the preview of the camera. You can use TextureView.SurfaceTextureListener to get a callback every time the surface changes. TextureView does provide a method getBitmap(Bitmap) which you can use to get the preview frame in the same size as the TextureView.

You can use this Google sample as starting point. Simply update the surfaceTextureListener like shown here:

private val surfaceTextureListener = object : TextureView.SurfaceTextureListener {    override fun onSurfaceTextureAvailable(texture: SurfaceTexture, width: Int, height: Int) {        openCamera(width, height)    }    override fun onSurfaceTextureSizeChanged(texture: SurfaceTexture, width: Int, height: Int) {        configureTransform(width, height)    }    override fun onSurfaceTextureDestroyed(texture: SurfaceTexture) = true    override fun onSurfaceTextureUpdated(texture: SurfaceTexture) {        // Start changes        // Get the bitmap        val frame = Bitmap.createBitmap(textureView.width, textureView.height, Bitmap.Config.ARGB_8888)        textureView.getBitmap(frame)        // Do whatever you like with the frame        frameProcessor?.processFrame(frame)        // End changes    }}


Use Below code to do so.

 CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);        try {            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());            Size[] jpegSizes = null;            if (characteristics != null) {                jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);            }            int width = 480;//480x320            int height = 320;            if (jpegSizes != null && 0 < jpegSizes.length) {                width = jpegSizes[0].getWidth();                height = jpegSizes[0].getHeight();            }            ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);            List<Surface> outputSurfaces = new ArrayList<Surface>(2);            outputSurfaces.add(reader.getSurface());            outputSurfaces.add(new Surface(textureView.getSurfaceTexture()));            final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);            captureBuilder.addTarget(reader.getSurface());            captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);            // Orientation            int rotation = ((Activity) context).getWindowManager().getDefaultDisplay().getRotation();            captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));            final File file = getFileDir();            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);                        save(bytes);                    } catch (FileNotFoundException e) {                        e.printStackTrace();                    } catch (IOException e) {                        e.printStackTrace();                    } finally {                        if (image != null) {                            image.close();                        }                    }                }                private void save(byte[] bytes) throws IOException {                    OutputStream output = null;                    try {                        output = new FileOutputStream(file);                        output.write(bytes);                    } finally {                        if (null != output) {                            output.close();                        }                    }                }            };            reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);        } catch (CameraAccessException e) {            e.printStackTrace();        }