1. 程式人生 > >演算法移植優化(二)android gpuimage使用

演算法移植優化(二)android gpuimage使用

1、在androidMainfest.xml中加入:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2、新增控制元件imageview、按鈕button進行訊息響應

3、圖片的顯示需要縮放:

            //讀取圖片,並顯示到控制元件imageView
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            Bitmap d= BitmapFactory.decodeFile(picturePath);
            int nh = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
            Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true);

            imageView.setImageBitmap(scaled);

1、從https://github.com/CyberAgent/android-gpuimage下載gpuimage,並把//src檔案目錄原始碼拷貝處理,與我們自己的專案src目錄合併。

2、在我們的src目錄下,有個build.gradle開啟,然後往dependencies中加入:

compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'

3、在mainactivity中加入gpu相關處理程式

//gpu 預處理
GPUImage mGPUImage = new GPUImage(this);
//mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.imageView));//
加入這句,程式會崩潰 mGPUImage.setImage(scaled); //設定gpu要處理的圖片 mGPUImage.setFilter(new GPUImageSobelEdgeDetection());//設定濾波方法 d = mGPUImage.getBitmapWithFilterApplied();//進行gpu影象處理,獲取處理後的圖片

4、mainactivity主要原始碼如下:

//讀取圖片,並顯示到控制元件imageView
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap d= BitmapFactory.decodeFile
(picturePath); int nh = (int) ( d.getHeight() * (512.0 / d.getWidth()) );//把圖片縮放到可以在螢幕上顯示 Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true); //gpu 預處理 GPUImage mGPUImage = new GPUImage(this); //mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.imageView));//加入這句,程式會崩潰 mGPUImage.setImage(scaled); //設定gpu要處理的圖片 mGPUImage.setFilter(new GPUImageSobelEdgeDetection());//設定濾波方法 d = mGPUImage.getBitmapWithFilterApplied();//進行gpu影象處理,獲取處理後的圖片 imageView.setImageBitmap(d);//設定view控制元件中的顯示內容