RAJENDRA PAWAR
RAJENDRA PAWAR

Reputation: 43

Getting Exception when setting Frame.Builder() in eclipse

I am implementing face detection example in eclipse but when set Frame.Bulder() ,its throwing exception .Following is the CameraSource class in which exception is thrown.

  public void run() {
        Frame outputFrame;
        ByteBuffer data;

        while (true) {
            synchronized (mLock) {
                if (mActive && (mPendingFrameData == null)) {
                    try {
                        // Wait for the next frame to be received from the camera, since we
                        // don't have it yet.
                        mLock.wait();
                    } catch (InterruptedException e) {
                        Log.d(TAG, "Frame processing loop terminated.", e);
                        return;
                    }
                }

                if (!mActive) {
                    // Exit the loop once this camera source is stopped or released.  We check
                    // this here, immediately after the wait() above, to handle the case where
                    // setActive(false) had been called, triggering the termination of this
                    // loop.
                    return;
                }

                outputFrame = new Frame.Builder()
                        .setImageData(mPendingFrameData, mPreviewSize.getWidth(),
                                mPreviewSize.getHeight(), ImageFormat.NV21)
                        .setId(mPendingFrameId)
                        .setTimestampMillis(mPendingTimeMillis)
                        .setRotation(mRotation)
                        .build();

                // Hold onto the frame data locally, so that we can use this for detection
                // below.  We need to clear mPendingFrameData to ensure that this buffer isn't
                // recycled back to the camera before we are done using that data.
                data = mPendingFrameData;
                mPendingFrameData = null;
            }

            // The code below needs to run outside of synchronization, because this will allow
            // the camera to add pending frame(s) while we are running detection on the current
            // frame.

            try {
                mDetector.receiveFrame(outputFrame);
            } catch (Throwable t) {
                Log.d(TAG, "Exception thrown from receiver.", t);
            } finally {
                mCamera.addCallbackBuffer(data.array());
            }
        }
    }
}

and following is the log error

01-27 10:08:10.368: E/AndroidRuntime(14778): FATAL EXCEPTION: Thread-268 01-27 10:08:10.368: E/AndroidRuntime(14778): Process: com.example.customcamera, PID: 14778 01-27 10:08:10.368: E/AndroidRuntime(14778): java.lang.IllegalArgumentException: Image byte buffer must be allocated as 'direct'. See ByteBuffer.allocateDirect(). 01-27 10:08:10.368: E/AndroidRuntime(14778): at com.google.android.gms.vision.Frame$Builder.setImageData(Unknown Source) 01-27 10:08:10.368: E/AndroidRuntime(14778): at com.example.customcamera.camera.CameraSource$FrameProcessingRunnable.run(CameraSource.java:1304) 01-27 10:08:10.368: E/AndroidRuntime(14778): at java.lang.Thread.run(Thread.java:818)

Upvotes: 3

Views: 709

Answers (1)

bomba1990
bomba1990

Reputation: 437

That demo requires compiling with Google Play Services 8.1. I'm guessing that you compiled with Google Play Services 7.8.

This was the result of a bug fix that occurred between the two versions. The issue is that using direct byte buffers with the CameraSource class will result in a pixel shift of the images sent to the detector (e.g., the position reported for the barcode may be shifted to the left several pixels). In 8.1 we added support for indirect byte buffers to address this, but the 7.8 library doesn't have that ability.

Long story short: Compile with 8.1 if you can. If you can't, you'll need to change to direct byte buffers as suggested above, and positions reported back will be off by several pixels.

Source: https://github.com/googlesamples/android-vision/issues/33

Upvotes: 3

Related Questions