首页 > 解决方案 > 获取 RGB 值实时相机,错误 NullPointerException

问题描述

[更新]:已添加屏幕截图。

我正在学习 opencv-3.4.1-android-sdk_4 的基本教程tutorial-1-camerapreview。我想点击屏幕并获取该位置的 RGB 颜色。我从HERE获得了帮助(从相机预览中的触摸事件中检索准确的 RGB 值)x y

获取 xy 坐标:

@SuppressWarnings("deprecation")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = (int)event.getX();
        y = (int)event.getY();
        return super.onTouchEvent(event);
    }

onCameraFrame为:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        if(x != -1 && y != -1) { //if this is true, you've touched something
            rgb = mRgba.get(x,y);

            Log.d(TAG, "Touch coordinates--> " + "x: " + String.valueOf(x)
                    + " y: " + String.valueOf(y) + " \n"
                    + "RGB values--> "
                    + " Red: "   + rgb[0]
                    + " Green: " + rgb[1]
                    + " Blue: "  + rgb[2]);


            x = -1;
            y = -1;
        }

        return mRgba;
    }

我收到这些错误:

E/AndroidRuntime:致命异常:Thread-904

java.lang.NullPointerException

在 test.com.imagecolortest.MainActivity.onCameraFrame(MainActivity.java:104)

在 org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)

在 org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)

在 java.lang.Thread.run(Thread.java:838)

笔记:

行号 104 (MainActivity.java:104)是:

  • " y: " + String.valueOf(y) + " \n"

有时它会在我单击屏幕左上角附近时运行。但是当我点击其他地方时它会崩溃。

有时,当它通过在屏幕上点击一键运行时,它会给出两个日志(触发两次):(通过用相机扫描图像)

06-22 18:09:24.515 416-806/test.com.imagecolortest D/OCVSample::Activity: Touch coordinates--> x: 340 y: 587 
                                                                             RGB values--> Red: 211.0 Green: 81.0 Blue: 220.0
06-22 18:09:24.613 416-806/test.com.imagecolortest D/OCVSample::Activity: Touch coordinates--> x: 340 y: 587 
                                                                             RGB values--> Red: 205.0 Green: 75.0 Blue: 217.0

有时红色rgb[0]值是0触发器的Log.d三倍。不知道为什么。

[编辑]:我们可以稍后解决这个问题,因为我们正在返回super.onTouchEvent(event)而不是返回true。稍后会添加。

我的代码:
MainActivity.java

package test.com.imagecolortest;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements CvCameraViewListener2 {
    private static final String TAG = "OCVSample::Activity";
    private CameraBridgeViewBase mOpenCvCameraView;
    private boolean              mIsJavaCamera = true;
    private MenuItem mItemSwitchCamera = null;
    private Mat mRgba;
    int x = -1, y = -1;
    double [] rgb;
    TextView touchView;

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS: {
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default: {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    public MainActivity() {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);

        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.activity_main);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
        touchView = (TextView)findViewById(R.id.textView);
    }

    @Override
    public void onPause(){
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume(){
        super.onResume();
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        } else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    public void onCameraViewStarted(int width, int height) {
        mRgba = new Mat(height, width, CvType.CV_8UC4);
    }

    public void onCameraViewStopped() {
        mRgba.release();
    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        if(x != -1 && y != -1) { //if this is true, you've touched something
            rgb = mRgba.get(x,y);

            Log.d(TAG, "Touch coordinates--> " + "x: " + String.valueOf(x)
                    + " y: " + String.valueOf(y) + " \n"
                    + "RGB values--> "
                    + "Red: " + rgb[0]
                    + " Green: " + rgb[1]
                    + " Blue: " + rgb[2]);

// touchView.setText("Touch coordinates--> " + "x: " + String.valueOf(x)
//                    + " y: " + String.valueOf(y) + " \n" + "RGB values--> " + "Red: " + rgb[0]
//                    + " Green: " + rgb[1] + " Blue: " + rgb[2]);
            x = -1;
            y = -1;
        }

        return mRgba;
    }

    //detects touches on screen
    @SuppressWarnings("deprecation")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = (int)event.getX();
        y = (int)event.getY();

        //Display display = getWindowManager().getDefaultDisplay();
        //int width = display.getWidth();
        //int height = display.getHeight();
        //double [] rgb = mRgba.get(width,height);


//        touchView.setText("Touch coordinates--> " + "x: " + String.valueOf(x)
//                + " y: " + String.valueOf(y) + " \n" + "RGB values--> " + "Red: " + rgb[0]
//                + " Green: " + rgb[1] + " Blue: " + rgb[2]);

        return super.onTouchEvent(event);
    }
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <org.opencv.android.JavaCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/activity_main"
        opencv:show_fps="true"
        opencv:camera_id="any" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView"
        android:layout_gravity="left|bottom" />


</FrameLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.com.imagecolortest">
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >
        <activity   android:name=".MainActivity"
                    android:screenOrientation="landscape"
                    android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <supports-screens android:resizeable="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true" />

</manifest>

[更新]

我想我没有将边界(宽度、高度、像素等)定义为click event. 就像我点击屏幕左侧一样,它可以工作,但是当我点击其他角落/侧面(离中心很远)时,应用程序崩溃了。我在屏幕上显示实时数据

Imgproc.putText() as: Imgproc.putText(mRgba,"R:"+rgb[0] + " G:"+rgb[1] +" B:"+rgb[2] + "x: " + x + " y: " + y, new Point(10,52), Core.FONT_HERSHEY_COMPLEX,.7, new Scalar(5,255,255),2,8,false ); 

我注意到一些非常奇怪的行为。(y垂直)值没问题。但是当我从左到右滑动手指时,该x值在 范围内效果很好0-599,当它触及600突然应用程序崩溃的值时。请参阅下面的照片: 截屏

14.93 FPS@800x600
R:134.0 G:155.0 B:151.0
x:371 y:488

标签: javaandroidopencvopencv4android

解决方案


CvCameraViewFrame.rgba() 返回一个 Mat,OpenCV Mat 索引首先采用行,然后是列。您正在调用rgb = mRgba.get(x,y);,因此您将 x 作为行传递,将 y 作为列传递,当 x >= 600 时,这将超出 800x600 显示的范围。

https://docs.opencv.org/java/2.4.9/org/opencv/core/Mat.html#get(int,%20int))。


推荐阅读