Android开发技巧
1、根据res路径来读取OpenCV的xml人脸检测文件
private CascadeClassifier cascadeClassifier; //加载人脸检测xml try { // Copy the resource into a temp file so OpenCV can load it InputStream is = getResources().openRawResource(R.raw.haarcascade_frontalface_alt); File cascadeDir = getDir("cascade", Context.MODE_PRIVATE); File mCascadeFile = new File(cascadeDir, "haarcascade_frontalface_alt.xml"); FileOutputStream os = new FileOutputStream(mCascadeFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); // Load the cascade classifier cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath()); } catch (Exception e) { Log.e("OpenCVActivity", "Error loading cascade", e); } |
2、菜单操作Menu
res-menu建菜单menu_main.xml xml version="1.0" encoding="utf-8"?> onOptionsItemSelected()方法内实现 @Override
//创建菜单 public boolean onCreateOptionsMenu(Menu menu){ |
3、Button按钮控件/子Activity开启
方式一: findViewById(R.id.btn_online_demo).setOnClickListener(MainActivity.this); @Override 方式二: buttonFirst = (Button)findViewById(R.id.button1);
|
4、
private File mPictureFile; …… // 设置相机拍照后照片保存路径 |
5、在Bitmap上绘制区域Canvas(科大讯飞)
private void drawFaceRects(FaceRect[] faces) { |
6、保存Bitmap图片到指定路径
//保存图片
|
7、随机生成指定长度的字符串
//length用户要求产生字符串的长度
|
8、进度对话框的使用
// 进度对话框 mProDialog = new ProgressDialog(this);
mProDialog.setMessage("注册中...");
if (null != mProDialog) { |
9、RadioGroup单选按钮控件
RadioGroup alignGruop = (RadioGroup) findViewById(R.id.align_mode);
|
10、Camera操作
private Camera mCamera;
nv21 = new byte[PREVIEW_WIDTH * PREVIEW_HEIGHT * 2];
@Override
private void openCamera() {
private void closeCamera() { private boolean checkCameraPermission() { // 长按SurfaceView 500ms后松开,摄相头聚集
|
11、读取指定路径的Bitmap
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.img_01);
|
12、double转成String
String str_x = Double.toString(pt.x);
|
13、创建线程
HandlerThreaddetectThread = null;
detectThread = new HandlerThread("detect");
detectHandler.post(new Runnable() {
|
14、复制Bitmap
Bitmap tmp;
|
15、Toast提示功能
Toast.makeText(MainActivity.this, "You Clicked Button1!", Toast.LENGTH_SHORT).show();
|
16、选择图片,并显示
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
@Override
|
17、NDK使用方法:http://blog.csdn.net/taily_duan/article/details/52484583
public static native int[] grayProc(int[] pixels, int w, int h);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
int w = bmp.getWidth();
|
18、跳转子页面
Button buttonThree = (Button)findViewById(R.id.button3);
|
19、ListView显示:图片+文字
ListView listView = (ListView) findViewById(R.id.listView1);
|
20、
|
21、
|
OpenCV for Android开发技巧
1、实现CvCameraViewListener 需要实现的虚函数,定义BaseLoaderCallback回调函数接口,并实现这个类中的onManagerConnected()方法
private CameraBridgeViewBase mOpenCvCameraView; private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(MainActivity.this) { @Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); imageMat=new Mat(); //enable camera mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } } };
|
2、onResume()中加载OpenCV库(OpenCVLoader);
@Override |
3、Mat变量创建
Mat mRgba = new Mat(height, width, CvType.CV_8UC4);//创建 mRgba.release();//释放
|
4、根据res路径来读取OpenCV的xml人脸检测文件
private CascadeClassifier cascadeClassifier; //加载人脸检测xml try { // Copy the resource into a temp file so OpenCV can load it InputStream is = getResources().openRawResource(R.raw.haarcascade_frontalface_alt); File cascadeDir = getDir("cascade", Context.MODE_PRIVATE); File mCascadeFile = new File(cascadeDir, "haarcascade_frontalface_alt.xml"); FileOutputStream os = new FileOutputStream(mCascadeFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); // Load the cascade classifier cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath()); } catch (Exception e) { Log.e("OpenCVActivity", "Error loading cascade", e); } |
5、人脸检测
private CascadeClassifier cascadeClassifier;
|
6、Bitmap与Mat转换/OpenCV彩色转灰度
//---------------------OpenCV处理------------------------ |
7、图片的缩放/或使用resize
//缩放图片大小 |
8、Mat中截取Rect部分的图片
Rect[] facesArray = faces.toArray(); Mat copyMat = mGray.submat(facesArray[i]);
|
9、使用Core.putText在图像上输入文字
String str = str_x + ", " + str_y + " S1:" + c1 + " S2:"+ c2;
|
10、Mat、Bitmap、IplImage相互转换
//Mat转成Bitmap
|
11、图像的灰度直方图
public double CmpPic(opencv_core.IplImage Image1, opencv_core.IplImage Image2) {
|
12、RGB转换成HSV
int nHeight = bitmap.getHeight()/10;
|
13、
|
14、
|
15、
|
16、
|
17、
|
18、
|
19、
|