图像识别:Android中使用OpenCV
16lz
2021-01-25
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:id="@+id/btnNDK"
- android:text="使用C++OpenCV进行处理"/>
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:id="@+id/btnRestore"
- android:text="还原"/>
- <ImageViewandroid:id="@+id/ImageView01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"/>
- </LinearLayout>
- packagecom.testopencv.haveimgfun;
- publicclassLibImgFun{
- static{
- System.loadLibrary("ImgFun");
- }
- /**
- *@paramwidththecurrentviewwidth
- *@paramheightthecurrentviewheight
- */
- publicstaticnativeint[]ImgFun(int[]buf,intw,inth);
- }
- #include<jni.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<opencv2/opencv.hpp>
- usingnamespacecv;
- extern"C"{
- JNIEXPORTjintArrayJNICALLJava_com_testopencv_haveimgfun_LibImgFun_ImgFun(
- JNIEnv*env,jobjectobj,jintArraybuf,intw,inth);
- JNIEXPORTjintArrayJNICALLJava_com_testopencv_haveimgfun_LibImgFun_ImgFun(
- JNIEnv*env,jobjectobj,jintArraybuf,intw,inth){
- jint*cbuf;
- cbuf=env->GetIntArrayElements(buf,false);
- if(cbuf==NULL)
- {
- return0;
- }
- Matmyimg(h,w,CV_8UC4,(unsignedchar*)cbuf);
- for(intj=0;j<myimg.rows/2;j++)
- {
- myimg.row(j).setTo(Scalar(0,0,0,0));
- }
- intsize=w*h;
- jintArrayresult=env->NewIntArray(size);
- env->SetIntArrayRegion(result,0,size,cbuf);
- env->ReleaseIntArrayElements(buf,cbuf,0);
- returnresult;
- }
- }
- LOCAL_PATH:=$(callmy-dir)
- include$(CLEAR_VARS)
- include../includeOpenCV.mk
- ifeq("$(wildcard$(OPENCV_MK_PATH))","")
- #trytoloadOpenCV.mkfromdefaultinstalllocation
- include$(TOOLCHAIN_PREBUILT_ROOT)/user/share/OpenCV/OpenCV.mk
- else
- include$(OPENCV_MK_PATH)
- endif
- LOCAL_MODULE:=ImgFun
- LOCAL_SRC_FILES:=ImgFun.cpp
- include$(BUILD_SHARED_LIBRARY)
- APP_STL:=gnustl_static
- APP_CPPFLAGS:=-frtti-fexceptions
- APP_ABI:=armeabiarmeabi-v7a
- packagecom.testopencv.haveimgfun;
- importandroid.app.Activity;
- importandroid.graphics.Bitmap;
- importandroid.graphics.Bitmap.Config;
- importandroid.graphics.drawable.BitmapDrawable;
- importandroid.os.Bundle;
- importandroid.widget.Button;
- importandroid.view.View;
- importandroid.widget.ImageView;
- publicclassHaveImgFunextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- ImageViewimgView;
- ButtonbtnNDK,btnRestore;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle("使用NDK转换灰度图");
- btnRestore=(Button)this.findViewById(R.id.btnRestore);
- btnRestore.setOnClickListener(newClickEvent());
- btnNDK=(Button)this.findViewById(R.id.btnNDK);
- btnNDK.setOnClickListener(newClickEvent());
- imgView=(ImageView)this.findViewById(R.id.ImageView01);
- Bitmapimg=((BitmapDrawable)getResources().getDrawable(R.drawable.lena)).getBitmap();
- imgView.setImageBitmap(img);
- }
- classClickEventimplementsView.OnClickListener{
- publicvoidonClick(Viewv){
- if(v==btnNDK)
- {
- longcurrent=System.currentTimeMillis();
- Bitmapimg1=((BitmapDrawable)getResources().getDrawable(R.drawable.lena)).getBitmap();
- intw=img1.getWidth(),h=img1.getHeight();
- int[]pix=newint[w*h];
- img1.getPixels(pix,0,w,0,0,w,h);
- int[]resultInt=LibImgFun.ImgFun(pix,w,h);
- BitmapresultImg=Bitmap.createBitmap(w,h,Config.RGB_565);
- resultImg.setPixels(resultInt,0,w,0,0,w,h);
- longperformance=System.currentTimeMillis()-current;
- imgView.setImageBitmap(resultImg);
- HaveImgFun.this.setTitle("w:"+String.valueOf(img1.getWidth())+",h:"+String.valueOf(img1.getHeight())
- +"NDK耗时"+String.valueOf(performance)+"毫秒");
- }
- elseif(v==btnRestore)
- {
- Bitmapimg2=((BitmapDrawable)getResources().getDrawable(R.drawable.lena)).getBitmap();
- imgView.setImageBitmap(img2);
- HaveImgFun.this.setTitle("使用OpenCV进行图像处理");
- }
- }
- }
- }
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- Android支持多种设备的方法及资源文件的使用
- Android开发笔记:图标尺寸的约定
- Android(安卓)环境配置,和一系列问题的解决方案
- AssetManager读取assets下多张图片资源输出到ImageView动画
- Android工程中调用ffmpeg