发现Android自带的图像处理还是不够强大,最近项目中遇到的图像处理相关难题,参照了下C++写的opencv,发现很强大,还提供了android sdk.赞!
下面我们来使用它:
首先,进入官网(http://opencv.org/)下载OpenCV4Android并解压。
具体Android Studio 配置请看http://www.cnblogs.com/tail/p/4618476.html
效果一:
Android open cv 使用记录_第1张图片 —->Android open cv 使用记录_第2张图片
效果二:
Android open cv 使用记录_第3张图片 —->Android open cv 使用记录_第4张图片

public class Tutorial1Activity extends Activity {    private static final String TAG = "OCVSample::Activity";    private ImageView drawView;    private Bitmap srcBitmap;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);          OpenCVLoader.initDebug();        setContentView(R.layout.tutorial1_surface_view);        drawView = (ImageView) findViewById(R.id.tutorial1_activity_java_surface_view);        Button button = (Button) findViewById(R.id.gray_button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                srcBitmap = ((BitmapDrawable)  drawView.getDrawable()).getBitmap();                drawView.setImageBitmap(procSrc2Gray());            }        });    }    //效果一:    public Bitmap procSrcGray(Bitmap srcBitmap){        Mat rgbMat = new Mat();        Mat grayMat = new Mat();        Bitmap grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);        //convert original bitmap to Mat, R G B.        Utils.bitmapToMat(srcBitmap, rgbMat);        //rgbMat to gray grayMat        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();        Imgproc.findContours(grayMat, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);        Imgproc.drawContours(rgbMat, contours, -1, new Scalar(255,255,255),-1);        Utils.matToBitmap(rgbMat, grayBitmap); //convert mat to bitmap        return  grayBitmap;    }        //效果二:    public Bitmap procSrc2Gray() {        Bitmap srcBitmap88 = Bitmap.createBitmap(srcBitmap).copy(Bitmap.Config.RGB_565, true);        Bitmap grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);        Mat gray = new Mat();        Mat bw = new Mat();        Mat src = new Mat();        //convert original bitmap to Mat, R G B.        Utils.bitmapToMat(srcBitmap88, src);        //rgbMat to gray grayMat        Imgproc.cvtColor(src, gray, Imgproc.COLOR_RGB2GRAY);        Imgproc.threshold(gray, bw, 150, 255, Imgproc.THRESH_BINARY_INV);        Mat temp = bw.clone();        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();        Imgproc.findContours(temp, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);        Imgproc.drawContours(bw, contours, -1, new Scalar(255, 255, 255), -1);        Mat rgb = new Mat();        Imgproc.cvtColor(bw, rgb, Imgproc.COLOR_GRAY2RGB);        //convert mat to bitmap        Utils.matToBitmap(rgb, grayBitmap);                 return grayBitmap;    }}

布局代码:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >    <ImageView  android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tutorial1_activity_java_surface_view" android:src="@drawable/bg1" />    <Button  android:id="@+id/gray_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="变灰"/></FrameLayout>

调用opencv的代码是在C++高手指导下完成的,感谢opencv,感谢C++.

更多相关文章

  1. android opengl es 正方体纹理效果
  2. Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存
  3. 实现TextView的垂直滚动以及通过代码设置TextView滚动无法显示滚
  4. Android Studio使用XML样式在JAVA代码中的使用(使用java代码调节x
  5. Android实现代码画虚线边框背景效果
  6. Android CRT Screen 电视效果
  7. Android 实现高斯模糊效果
  8. ProgressBar:自定义旋转图片

随机推荐

  1. Android中的网络时间同步
  2. Android系统服务 WMS与AMS简单理解
  3. android window窗口机制
  4. Android应用程序防止被LMK干掉
  5. mysql 5.7.16 免安装版安装配置方法图文
  6. mysql一对多关联查询分页错误问题的解决
  7. mysql 5.7.23 安装配置图文教程
  8. mysql 5.7.23 winx64解压版安装教程
  9. Mysql提升大数据表拷贝效率的解决方案
  10. mysql 5.7.21 winx64绿色版安装配置方法