我是上下文android的初学者。我已经写了一个android项目,它使用opencv从前置摄像头捕获帧,最后只需借助2个按钮转换灰度或rbga帧。想到的是,当点击按钮“GrayScale”时,图像将以灰度等转换为rgba,但是当我在项目中添加按钮时,我的应用程序就会崩溃。
java文件,基本方法:
//variables
protected static final String TAG = null;
private Camera camera;
private CameraBridgeViewBase mOpenCvCameraView;
private CvCameraViewFrame inputFrame;
private int CameraMod,GrayScale=0,RGBA=0;
private Button button1,button2;
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;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.i(TAG,"called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
button1 = (Button) findViewById(R.id.GrayScale);
button2 = (Button) findViewById(R.id.RGBA);
button1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
GrayScale=1;
RGBA=0;
};
});
button2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
RGBA=1;
GrayScale=0;
};
});
setContentView(R.layout.activity_main);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
CameraMod=camera.getNumberOfCameras();
if (CameraMod >= 2)
{
mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
}
else
{
mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);
}
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
{
Mat image = null;
if (GrayScale==1)
{
image = inputFrame.gray();
}
if (RGBA==1)
{
image = inputFrame.rgba();
}
if (CameraMod >= 2)
{
Core.flip(image, image, 1);
}
return image;
};
xml文件包含按钮:
<LinearLayout 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" >
<Button
android:text="GrayScale"
android:id="@+id/GrayScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="RGBA"
android:id="@+id/RGBA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<org.opencv.android.JavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:id="@+id/HelloOpenCvView"
opencv:show_fps="true"
opencv:camera_id="any" />
答案 0 :(得分:1)
您在通过setContentView()
查看您的观看次数后致电findViewById()
,这是错误的。您的视图(即按钮)必须为null。在通过setContentView()
查找视图之前,请务必致电findViewById()
。
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.i(TAG,"called onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Other code
}