美文网首页
Android Studio 2.1 + javaCV 1.2

Android Studio 2.1 + javaCV 1.2

作者: 王世军Steven | 来源:发表于2016-09-20 16:55 被阅读1821次

    1, 下载并解压 javaCV 1.2

    JavaCV 官方网站
    地址 : https://github.com/bytedeco/javacv

    下载选项

    解压下载文件, 内容如下图

    2.png

    2, 打开Android Studio 新建项目.

    3, 将以下包拷贝到"\TestJavaCV\app\libs"文件夹下.

    3.png

    4, 在app上按"F4"打开 Project Structure 页面.

    4.1 选择 Dependencies , 如下图.

    4.png

    4.2 点击右侧 "+",选择"2 File Dependency". 将刚才拷贝的".jar"文件添加进去.

    5.png

    5, 在"app/build.gradle"中添加如下代码

    android {
        //Exclude duplicates
        packagingOptions {
            exclude 'META-INF/services/javax.annotation.processing.Processor'
            pickFirst  'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.properties'
            pickFirst  'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.xml'
            pickFirst  'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.properties'
            pickFirst  'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.xml'
        }
    }
    
    6.png

    注意 : 如果不添加上面的代码会出现一下错误

    错误信息

    5, 测试程序

    package com.wsj.custemview.testjavacv;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ImageView;
    
    import org.bytedeco.javacpp.opencv_core;
    import org.bytedeco.javacpp.opencv_core.IplImage;
    
    // 静态成员变量必须 static导入.
    import static org.bytedeco.javacpp.opencv_core.IPL_DEPTH_8U;
    import static org.bytedeco.javacpp.opencv_core.cvFlip;
    
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ImageView imageView = (ImageView) findViewById(R.id.img1);
            // Drawable drawable = idToDrawable(R.mipmap.ic_launcher);
    
            // Bitmap bitmap = this.drawableToBitmap(drawable);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
            IplImage iplImage = this.bitmapToIplImage(bitmap);
            cvFlip(iplImage,iplImage,0);
            bitmap = this.IplImageToBitmap(iplImage);
            imageView.setImageBitmap(bitmap);
        }
        /**
         * IplImage转化为Bitmap
         * @param iplImage
         * @return
         */
        public Bitmap IplImageToBitmap(opencv_core.IplImage iplImage) {
            Bitmap bitmap = null;
            bitmap = Bitmap.createBitmap(iplImage.width(), iplImage.height(),
                    Bitmap.Config.ARGB_8888);
            bitmap.copyPixelsFromBuffer(iplImage.getByteBuffer());
            return bitmap;
        }
    
        /**
         * Bitmap转化为IplImage
         * @param bitmap
         * @return
         */
        public opencv_core.IplImage bitmapToIplImage(Bitmap bitmap) {
            opencv_core.IplImage iplImage;
            iplImage = IplImage.create(bitmap.getWidth(), bitmap.getHeight(),
                    IPL_DEPTH_8U, 4);
            bitmap.copyPixelsToBuffer(iplImage.getByteBuffer());
            return iplImage;
        }
    
        /**
         * 将资源ID转化为Drawable
         * @param id
         * @return
         */
        public Drawable idToDrawable(int id) {
            return this.getResources().getDrawable(id);
        }
    
        /**
         * 将Drawable转化为Bitmap
         * @param drawable
         * @return
         */
        public Bitmap drawableToBitmap(Drawable drawable) {
            if(drawable == null)
                return null;
            return ((BitmapDrawable)drawable).getBitmap();
        }
    }
    

    相关文章

      网友评论

          本文标题:Android Studio 2.1 + javaCV 1.2

          本文链接:https://www.haomeiwen.com/subject/jesoettx.html