在eclipse中设置Frame.Builder()时获取异常

时间:2016-01-27 04:54:29

标签: java android eclipse

我正在eclipse中实现面部检测示例,但是当设置Frame.Bulder()时,它抛出异常。以下是抛出异常的CameraSource类。

  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());
            }
        }
    }
}

以下是日志错误

  

01-27 10:08:10.368:E / AndroidRuntime(14778):致命异常:Thread-268   01-27 10:08:10.368:E / AndroidRuntime(14778):进程:com.example.customcamera,PID:14778   01-27 10:08:10.368:E / AndroidRuntime(14778):java.lang.IllegalArgumentException:必须将图像字节缓冲区分配为' direct'。请参见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:8:10.368:E / AndroidRuntime(14778):at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:3)

  

该演示需要使用Google Play Services 8.1进行编译。我   猜测你是用Google Play Services 7.8编译的。

     

这是两者之间发生错误修复的结果   版本。问题是使用直接字节缓冲区   CameraSource类将导致发送到的图像的像素移位   检测器(例如,为条形码报告的位置可以是   左移几个像素)。在8.1中我们添加了支持   间接字节缓冲区来解决这个问题,但7.8库没有   有这种能力。

     

长话短说:如果可以的话,用8.1编译。如果你不能,你会   需要更改为直接字节缓冲区,如上所述,和   报告的位置将偏离几个像素。

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