gpt4 book ai didi

java - 获取 RGB 值实时相机,错误 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:41 26 4
gpt4 key购买 nike

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

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

获取 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: 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)

注意:

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

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

还有

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

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

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>

[更新]

我认为我没有为点击事件定义边界(宽度、高度、像素等)。当我点击屏幕的左侧时,它起作用了,但是当我点击其他角/边时(离中心很远),应用程序崩溃了。我在屏幕上显示实时数据

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 值时突然应用程序崩溃。请参阅下面附上的照片: Screenshot

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

  • 红点显示我的手指现在所在的位置。
  • 红色矩形 (实际上是正方形) 表明,当我触摸它的内部时,我的应用程序可以正常工作,但当它超出(到最右边) 时,它会崩溃。

最佳答案

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

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

关于java - 获取 RGB 值实时相机,错误 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50988869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com