码迷,mamicode.com
首页 > 移动开发 > 详细

Android opencv使用

时间:2020-10-21 20:55:24      阅读:33      评论:0      收藏:0      [点我收藏+]

标签:source   stop   end   java   creat   conf   track   rgb2gray   resources   

1.

    implementation ‘com.quickbirdstudios:opencv:4.3.0‘

2.

package chenlong.chenlong.java_opencv;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    SeekBar seekBar2;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar2=findViewById(R.id.seekBar2);
        textView=findViewById(R.id.textView);



        final ImageView imageView=(ImageView)findViewById(R.id.imageView);



        if (!OpenCVLoader.initDebug())
            Log.e("OpenCv", "Unable to load OpenCV");
        else
            Log.d("OpenCv", "OpenCV loaded");
        //mat变换.opencv do use mat show in ANDROID


        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                final Bitmap bitmapNew=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.aa);
//                final Bitmap bitmapNew = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                textView.setText(progress+"");

                Mat src = new Mat(bitmapNew.getHeight(), bitmapNew.getWidth(), CvType.CV_8UC4);

                Utils.bitmapToMat(bitmapNew, src);// turn bitmap into mat
                Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2GRAY);//灰色
//Gaussian Filters
                Imgproc.GaussianBlur(src, src, new Size(1, 1), progress, progress);//模糊

                Mat dst = Mat.zeros(new Size(src.width(), src.height()), CvType.CV_8UC3);

                Imgproc.threshold(src, dst, progress, progress,
                         Imgproc.THRESH_OTSU);

                final Bitmap bitret = bitmapNew.copy(Bitmap.Config.ARGB_8888, true);

                Utils.matToBitmap(dst, bitret);


                imageView.setImageBitmap(bitret);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {


            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
}

 

package chenlong.chenlong.java_opencv;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
SeekBar seekBar2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar2=findViewById(R.id.seekBar2);
textView=findViewById(R.id.textView);



final ImageView imageView=(ImageView)findViewById(R.id.imageView);



if (!OpenCVLoader.initDebug())
Log.e("OpenCv", "Unable to load OpenCV");
else
Log.d("OpenCv", "OpenCV loaded");
//mat变换.opencv do use mat show in ANDROID


seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

final Bitmap bitmapNew=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.aa);
// final Bitmap bitmapNew = bitmap.copy(Bitmap.Config.ARGB_8888, true);
textView.setText(progress+"");

Mat src = new Mat(bitmapNew.getHeight(), bitmapNew.getWidth(), CvType.CV_8UC4);

Utils.bitmapToMat(bitmapNew, src);// turn bitmap into mat
Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2GRAY);//灰色
//Gaussian Filters
Imgproc.GaussianBlur(src, src, new Size(1, 1), progress, progress);//模糊

Mat dst = Mat.zeros(new Size(src.width(), src.height()), CvType.CV_8UC3);

Imgproc.threshold(src, dst, progress, progress,
Imgproc.THRESH_OTSU);

final Bitmap bitret = bitmapNew.copy(Bitmap.Config.ARGB_8888, true);

Utils.matToBitmap(dst, bitret);


imageView.setImageBitmap(bitret);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {


}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

}
}

Android opencv使用

标签:source   stop   end   java   creat   conf   track   rgb2gray   resources   

原文地址:https://www.cnblogs.com/ksxh/p/13851471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!