问题
[UPDATE]: Screenshot has been added.
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):
Getting x y coordinates:
@SuppressWarnings("deprecation")
@Override
public boolean onTouchEvent(MotionEvent event) {
x = (int)event.getX();
y = (int)event.getY();
return super.onTouchEvent(event);
}
onCameraFrame as:
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;
}
I am getting these errors:
E/AndroidRuntime: FATAL EXCEPTION: Thread-904
java.lang.NullPointerException
at test.com.imagecolortest.MainActivity.onCameraFrame(MainActivity.java:104)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
at java.lang.Thread.run(Thread.java:838)
Note:
Line no. 104 (MainActivity.java:104) is:
- " y: " + String.valueOf(y) + " \n"
Also
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
and sometime when Red rgb[0]
value is 0
the Log.d
triggers three times. Don't know why.
[Edit]: We can address this issue latter as we are returning super.onTouchEvent(event)
instead returning true
. Will add that later.
My Code:
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]
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 );
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
- 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.
回答1:
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)).
来源:https://stackoverflow.com/questions/50988869/getting-rgb-value-live-camera-error-nullpointerexception