获取RGB值实时摄像机,错误NullPointerException [英] Getting RGB Value Live Camera, Error NullPointerException
问题描述
[更新]::已添加屏幕截图.
[UPDATE]: Screenshot has been added.
我从 opencv-3.4.1-android-sdk_4遵循基本教程tutorial-1-camerapreview
.
我想在屏幕上单击以获取该x
y
位置的RGB颜色.
我从 HERE 获得了帮助(从相机预览中的触摸事件中检索确切的RGB值):
I was following basic tutorial tutorial-1-camerapreview
from opencv-3.4.1-android-sdk_4.
I want to click on screen and get RGB Color of that x
y
position.
I got help from HERE (Retrieve exact RGB value from touch event in camera preview):
获取x y坐标:
@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
E/AndroidRuntime: FATAL EXCEPTION: Thread-904
java.lang.NullPointerException
java.lang.NullPointerException
在test.com.imagecolortest.MainActivity.onCameraFrame(MainActivity.java:104)
at test.com.imagecolortest.MainActivity.onCameraFrame(MainActivity.java:104)
在org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
在org.opencv.android.JavaCameraView $ CameraWorker.run(JavaCameraView.java:373)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
在java.lang.Thread.run(Thread.java:838)
at java.lang.Thread.run(Thread.java:838)
注意:
行号104 (MainActivity.java:104)是:
- "y:" + String.valueOf(y)+"\ n"
- " y: " + String.valueOf(y) + " \n"
也
有时它会像我单击屏幕左上角附近时一样运行.但是,当我单击其他位置时,它会崩溃.
Sometime it runs as when I click near to Top Left corner of screen it works. But when I click on other places it crashes.
有时,通过触摸屏幕上的一键运行它时,它会给出两个日志(触发两次) :(通过扫描
Sometimes when its run by touching one tap on screen it gives two logs (trigger two times): (by scanning this image with camera)
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
触发三遍.不知道为什么.
and sometime when Red rgb[0]
value is 0
the Log.d
triggers three times. Don't know why.
:我们稍后可以解决此问题,因为我们返回super.onTouchEvent(event)
而不是返回true
.稍后会添加.
: We can address this issue latter as we are returning super.onTouchEvent(event)
instead returning true
. Will add that later.
我的代码:
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>
[UPDATE]
我认为我没有为click event
定义边界(宽度,高度,像素等).就像我点击屏幕左侧 一样,但是当我点击其他角落/侧面(离中心很远)时,应用崩溃了.
我正在使用
I think I am not defining boundaries (width, height, pixels etc) to click event
. As when I click on left side of screen it works, but when I click on other corners/sides (very far from center) the app crashes.
I am displaying live data on screen with
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
的值时,应用突然崩溃.
请参见下面的照片附件:
and I have noted some very strange behavior. The y
(vertical) value is okay. But when I am sliding my finger from left to right the x
value works good at range of 0-599
, when it touches value of 600
suddenly app crashes.
Please see attached below photo:
14.93 FPS @ 800x600
R:134.0 G:155.0 B:151.0
x:371 y:488
14.93 FPS@800x600
R:134.0 G:155.0 B:151.0
x: 371 y: 488
- 红点显示,我的手指现在在哪里.
- 红色矩形(实际上是正方形)显示,当我触摸此矩形时,我的应用程序可以正常工作,但当它超出(最右侧)时,它就会崩溃.
- The Red dot shows, where my finger is right now.
- The Red Rectangle (actually square) shows that when I touching inside this, my app works but when it goes beyond (to very right) it crashes.
推荐答案
CvCameraViewFrame.rgba()返回Mat,OpenCV Mat索引首先排在行,然后排在列.您正在调用rgb = mRgba.get(x,y);
,因此您将x作为行,将y作为列,当x> = 600时,对于800x600的显示,这将超出范围.
CvCameraViewFrame.rgba() returns a Mat, and OpenCV Mat indexing takes row first, then column. You're calling rgb = mRgba.get(x,y);
so you're passing x as the row and y as the column, which will be out of bounds for an 800x600 display when x >= 600.
https ://docs.opencv.org/java/2.4.9/org/opencv/core/Mat.html#get(int,%20int)).
这篇关于获取RGB值实时摄像机,错误NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!