我正在使用OpenCV安卓库编写应用程序。我创建了一个我想要调用的CameraActivity,并返回一个图像。
public class CameraActivity extends Activity implements CvCameraViewListener2 {
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
public void onCreate(Bundle savedInstanceState) {
...
Button snap = (Button) findViewById(R.id.snap);
snap.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
processVideo();
}
});
snap.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
processPicture();
return true;
}
});
}
@Override
public void onPause() {
...
}
@Override
public void onResume() {
...
}
public void onDestroy() {
...
}
public void onCameraViewStarted(int width, int height) {
...
}
@Override
public void onCameraViewStopped() {
...
}
public Mat onCameraFrame(CvCameraViewFrame cvf) {
// Grab frame
cvf.rgba().copyTo(frame);
// Filter frame
filterLaser();
// Retrieve contour center point
contourLaser();
// Draw
draw();
// Display the result.
return cvf.rgba();
}
private void draw(){
...
}
private void filterLaser(){
...
}
void processVideo(){
...
}
void processPicture(){
Intent result = new Intent();
if(frame != null)
{
// Convert the processed Mat to Bitmap
Bitmap resultBitmap = Bitmap.createBitmap(frame.cols(), frame.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(frame, resultBitmap);
setResult(Activity.RESULT_OK, result);
result.putExtra("BITMAP", resultBitmap);
} else {
setResult(Activity.RESULT_CANCELED); // No frame found
}
Log.d("LastPoint", "FINISHED");
finish();
}
};
使用MainActivity中的以下代码调用此Activity。
Button reply = (Button) findViewById(R.id.buttonReply); reply.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(getApplicationContext(), CameraActivity.class); startActivityForResult(i, CAMERAREQUEST); } });
结果在MainActivity中提取。
@覆盖 public void onActivityResult(int requestCode,int resultCode,Intent data){ Log.d(" LastPoint","写作");
switch(requestCode) {
case (CAMERAREQUEST) :
if (resultCode == Activity.RESULT_OK) {
Bitmap bmp = (Bitmap) data.getParcelableExtra("Bitmap");
writeBitmap(bmp);
}
break;
}
}
" FINISHED"被打印到logcat。 "写" ISN'吨。 为什么我不能退出CameraActivity?视图本身也不会关闭。
答案 0 :(得分:4)
问题在于:
result.putExtra("BITMAP", resultBitmap);
通过将图片保存到我的CameraActivity中的sdcard并将Uri传递给结果意图解决了这个问题。