我正在使用OpenCV 3.0在Android中制作一个简单的相机应用程序。我读过有关Imgcodecs.imwrite但我无法使用它保存图片。这是我的代码:
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
{
@Override
public void onManagerConnected(int status)
{
switch (status)
{
case LoaderCallbackInterface.SUCCESS:
{
mOpenCvCameraView.enableView();
}
break;
default:
{
super.onManagerConnected(status);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
//Set Activity layout for Camera Preview
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_camera_home);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Load Camera Surface
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.camera_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
//Declare Clicking Button parameters
ImageButton capture = (ImageButton)findViewById(R.id.imageButton);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
//Capture Image by Button Click
capture.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
mp.start(); //Play Capture Sound
//Save Image by Button Click
sdRoot = Environment.getExternalStorageDirectory();
dir = "/DCIM/Data Collection/";
fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()).toString() + ".png";
File mkDir = new File(sdRoot, dir);
mkDir.mkdirs();
File pictureFile = new File(sdRoot, dir + fileName);*/
Imgcodecs.imwrite("/sdcard/" + dir + fileName,mGray);
}
});
}
@Override
public void onPause()
{
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
} else {
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);
mIntermediateMat = new Mat(height, width, CvType.CV_8UC4);
mGray = new Mat(height, width, CvType.CV_8UC1);
}
public void onCameraViewStopped()
{
mRgba.release();
mGray.release();
mIntermediateMat.release();
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
{
mRgba = inputFrame.rgba();
int maxValue = 255;
int blockSize = 5;
int meanOffset = 4;
Imgproc.adaptiveThreshold(inputFrame.gray(), mIntermediateMat, maxValue, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, blockSize, meanOffset);
Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
return mRgba;
}
它只是以二进制模式打开相机表面,我想保存二进制图像。