我有一张二进制图片。现在,我将如何点击按钮我想采取它。请帮助我。
这是我的代码:
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2, Camera.PictureCallback {
private static final String TAG = "MainActivity";
private JavaCameraView mOpenCvCameraView;
private static final int REQUEST_CODE = 123;
private Button mButton;
private Mat mRgba;
private Mat mByte;
private Mat imGgray, imgCandy;
CameraBridgeViewBase.CvCameraViewFrame inputFrame;
BaseLoaderCallback mBaseLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
super.onManagerConnected(status);
switch (status) {
case BaseLoaderCallback.SUCCESS:
{
mOpenCvCameraView.enableView();
break;
}
default:
{
super.onManagerConnected(status);
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.detetionbutton);
mOpenCvCameraView = (JavaCameraView) findViewById(R.id.textdetetion_view);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mButton.setOnClickListener(new View.OnClickListener() { // take a picture
@Override
public void onClick(View v) {
}
});
}
@Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
}
@Override
public void onResume() {
super.onResume();
if (OpenCVLoader.initDebug() == true) {
Log.i(TAG, "opencv loaded");
mBaseLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
} else {
Log.i(TAG, "opencn not loade");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mBaseLoaderCallback);
}
}
@Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
mByte = new Mat(height, width, CvType.CV_8UC4);
imGgray = new Mat(height, width, CvType.CV_8UC4);
imgCandy = new Mat(height, width, CvType.CV_8UC1);
}
@Override
public void onCameraViewStopped() {
mRgba.release();
}
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
Imgproc.cvtColor(mRgba, imGgray, Imgproc.COLOR_RGB2GRAY);
Imgproc.adaptiveThreshold(imGgray, mByte, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 35, 5);
return mByte; // this is m Binary image
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {}
}
答案 0 :(得分:2)
参考您的示例代码。你可以用两种方式做到这一点,
您可以使用OpenCV onPictureTaken 方法捕获图像并将其存储在存储中。
public void onPictureTaken(byte[] data, Camera camera) {
//Log.i(TAG, "Saving a bitmap to file");
// The camera preview was automatically stopped. Start it again.
mCamera.startPreview();
mCamera.setPreviewCallback(this);
// Write the image in a file (in jpeg format)
try {
FileOutputStream fos = new FileOutputStream(mPictureFileName);
fos.write(data);
fos.close();
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
}
并将此代码放在 onClick <\ n>侦听器
中mOpenCvCameraView.takePicture(fileName);
但问题是,作为预览发送到相机的图像将被保存为默认值(如果其RBG图像然后将保存RGB图像,如果其二进制文件将保存,则保存为二进制图像)
< / LI>或者您可以在Android上创建一个按钮,在 onClick 监听器中,您可以使用 imwrite 并保存图像,如果您的相机支持onTouch捕获,您也可以在 onTouch 方法中使用它。将此代码粘贴到 onClick 侦听器或 onTouch 侦听器内(您可以选择)。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String currentDateandTime = sdf.format(new Date());
String fileName = Environment.getExternalStorageDirectory().getPath() +
"/sample_picture_" + currentDateandTime + ".jpg";
Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
String filename = "/storage/emulated/0/DCIM/Camera/samplepass.jpg";
Highgui.imwrite(filename,mByte);