android bitmap 饱和度 demo,Android GPUImage实现多种图像滤镜效果
前言
GPUImage 是iOS下一個開源的基于GPU的圖像處理庫,提供各種各樣的圖像處理濾鏡,并且支持照相機和攝像機的實時濾鏡。GPUImage for Android是它在Android下的實現,同樣也是開源的。其中提供了幾十多種常見的圖片濾鏡API,且其機制是基于GPU渲染,處理速度相應也比較快,是一個不錯的圖片實時處理框架。
基本使用
GPUImage加載圖片有兩種方式,一種是通過GPUImage類獲取結果數據然后用ImageView加載,另一種是使用它的GPUImageView控件來進行展示。
GPUImage濾鏡素材
1.通過GPUImage類獲取結果數據然后用GLSurfaceView加載
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class GPUImageActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpu_image);
Uri imageUri = Uri.parse("https://upload-images.jianshu.io/upload_images/16311248-4ee6c079e02773d1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240");
GPUImage gpuImage = new GPUImage(this);
gpuImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));
gpuImage.setImage(imageUri); // this loads image on the current thread, should be run in a thread
gpuImage.setFilter(new GPUImageSketchFilter());
}
}
當然,你可能由于項目原因,需要使用ImageView來加載,GPUImage還提供了getBitmapWithFilterApplied接口,可以獲取到設置完濾鏡之后的Bitmap,你再調用ImageView的setImageBitmap也是可以的:
Uri imageUri = Uri.parse("https://upload-images.jianshu.io/upload_images/16311248-4ee6c079e02773d1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240");
GPUImage gpuImage = new GPUImage(this);
gpuImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));
gpuImage.setImage(imageUri);
gpuImage.setFilter(new GPUImageSketchFilter());
imageView.setImageBitmap(gpuImage.getBitmapWithFilterApplied());
2.通過GPUImageView控件來進行展示
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/gpu_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
public class GPUImageActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpu_image);
Uri imageUri = Uri.parse("https://upload-images.jianshu.io/upload_images/16311248-4ee6c079e02773d1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240");
GPUImageView gpuImageView = findViewById(R.id.gpu_image_view);
gpuImageView.setImage(imageUri);
gpuImageView.setFilter(new GPUImageSketchFilter());
}
}
這里添加了一個簡單的素描效果,我們看下加了濾鏡之后的效果圖:
素描效果
效果還不錯,如果我們想要換一種濾鏡,只要通過setFilter替換具體的濾鏡類就可以了,比如說我們調用gpuImageView.setFilter(new GPUImageGrayscaleFilter());換成黑白效果:
黑白濾鏡效果圖
具體的濾鏡類都在jp.co.cyberagent.android.gpuimage.filter這個package下,感興趣的朋友可以自行查閱。
調整亮度飽和度
剛才舉了兩個簡單的濾鏡效果,GPUImage還有一些濾鏡是有帶參構造方法的,用來方便我們調整效果的百分比,比如說調整飽和度和亮度。
飽和度
GPUImage調整飽和度是通過GPUImageSaturationFilter(float saturation),saturation代表當前所要設置的飽和度,范圍是0~1:
public class GPUImageActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
GPUImageView mGpuImageView;
AppCompatSeekBar mSeekBar;
TextView mProgressTv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpu_image);
mProgressTv = findViewById(R.id.progress_tv);
mSeekBar = findViewById(R.id.seek_bar);
mSeekBar.setMax(100);
mSeekBar.setOnSeekBarChangeListener(this);
Uri imageUri = Uri.parse("https://upload-images.jianshu.io/upload_images/16311248-4ee6c079e02773d1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240");
mGpuImageView = findViewById(R.id.gpu_image_view);
mGpuImageView.setImage(imageUri);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float value = (float)progress / 100f;
mProgressTv.setText("當前飽和度: " + value);
mGpuImageView.setFilter(new GPUImageSaturationFilter(value));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
代碼很簡單,就是根據seekBar的值去更新濾鏡的參數,呈現出來的效果:
飽和度調整.gif
亮度
亮度與飽和度用法相似,只需將setFilter替換為GPUImageBrightnessFilter(float brightness),代碼就不貼出來了,直接看效果:
亮度調整.gif
支持的圖片類型
可以看到剛才我們的demo是加載了一張網絡圖,GPUImage還支持其他的加載類型,除了網絡資源,還可以加載本地圖片也可以加載項目里的資源,比如Bitmap、File、Asset等等,在GPUImage類中可以看到這些接口:
/**
* Sets the image on which the filter should be applied.
*
* @param bitmap the new image
*/
public void setImage(final Bitmap bitmap) {
gpuImage.setImage(bitmap);
}
/**
* Sets the image on which the filter should be applied from a Uri.
*
* @param uri the uri of the new image
*/
public void setImage(final Uri uri) {
gpuImage.setImage(uri);
}
/**
* Sets the image on which the filter should be applied from a File.
*
* @param file the file of the new image
*/
public void setImage(final File file) {
gpuImage.setImage(file);
}
結語
GPUImage 的一些效果基本能滿足要求,其他的濾鏡效果,小伙伴們可以試試效果如何。其內部也是通過OpenGL實現的,待有空研究一下。
關于作者
GitHub:GitHub-ZJYWidget
CSDN博客:IT_ZJYANG
簡 書:Android小Y
在Github上建了一個集合炫酷自定義View的項目,里面有很多實用的自定義View源碼及demo,會長期維護,歡迎Star~ 如有不足之處或建議還望指正,相互學習,相互進步,如果覺得不錯動動小手給個喜歡, 謝謝~
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的android bitmap 饱和度 demo,Android GPUImage实现多种图像滤镜效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 小球效果,Android
- 下一篇: Borland C++ Builder必