Sceneform-android-sdk: How can I get CameraSource(or frame source) from Sceneform?

Created on 25 May 2018  路  1Comment  路  Source: google-ar/sceneform-android-sdk

Hi, I want to read QR Code from sceneform ArSceneView with BarcodeDetector.

but I can't get CameraSource.

Is there any method to get frame source? (or possible way to read QR Code in Sceneform session?)

Of course, I can get bitmap from surfaceview, but it is too slow.

question

Most helpful comment

You can access the camera stream from ARCore's frame object. What I did was

Register the listener to be called every frame
sceneView.getScene().setOnUpdateListener(this::onSceneUpdate);

Then you in onSceneUpdate you can access the ARCore frame and copy the image by calling acquireCameraImage(). This example is the "computervision" code from ARCore. From the image object you should be able to get the buffer to feed to the barcode detector.

  public void onSceneUpdate(FrameTime frameTime) {
        if (session == null) {
            return;
        }

        Frame frame = sceneView.getArFrame();
        if (frame == null) {
            return;
        }
        // Copy the camera stream to a bitmap
        try (Image image = frame.acquireCameraImage()) {
            if (image.getFormat() != ImageFormat.YUV_420_888) {
                throw new IllegalArgumentException(
                        "Expected image in YUV_420_888 format, got format " + image.getFormat());
            }

            ByteBuffer processedImageBytesGrayscale =
                    edgeDetector.detect(
                            image.getWidth(),
                            image.getHeight(),
                            image.getPlanes()[0].getRowStride(),
                            image.getPlanes()[0].getBuffer());

            Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(),
                    Bitmap.Config.ALPHA_8);
            processedImageBytesGrayscale.rewind();
            bitmap.copyPixelsFromBuffer(processedImageBytesGrayscale);
        } catch (Exception e) {
            Log.e(TAG, "Exception copying image", e);
        }
}

>All comments

You can access the camera stream from ARCore's frame object. What I did was

Register the listener to be called every frame
sceneView.getScene().setOnUpdateListener(this::onSceneUpdate);

Then you in onSceneUpdate you can access the ARCore frame and copy the image by calling acquireCameraImage(). This example is the "computervision" code from ARCore. From the image object you should be able to get the buffer to feed to the barcode detector.

  public void onSceneUpdate(FrameTime frameTime) {
        if (session == null) {
            return;
        }

        Frame frame = sceneView.getArFrame();
        if (frame == null) {
            return;
        }
        // Copy the camera stream to a bitmap
        try (Image image = frame.acquireCameraImage()) {
            if (image.getFormat() != ImageFormat.YUV_420_888) {
                throw new IllegalArgumentException(
                        "Expected image in YUV_420_888 format, got format " + image.getFormat());
            }

            ByteBuffer processedImageBytesGrayscale =
                    edgeDetector.detect(
                            image.getWidth(),
                            image.getHeight(),
                            image.getPlanes()[0].getRowStride(),
                            image.getPlanes()[0].getBuffer());

            Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(),
                    Bitmap.Config.ALPHA_8);
            processedImageBytesGrayscale.rewind();
            bitmap.copyPixelsFromBuffer(processedImageBytesGrayscale);
        } catch (Exception e) {
            Log.e(TAG, "Exception copying image", e);
        }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

LukasStancikas picture LukasStancikas  路  3Comments

KamikX picture KamikX  路  4Comments

kunal-wayfair picture kunal-wayfair  路  3Comments

rajaselvan picture rajaselvan  路  4Comments

arilotter picture arilotter  路  3Comments