当前位置: 源码素材网 » Android教程 » 详情页

Android为视频拍摄增加复古、浮雕等滤镜效果

  •   时间:2019-09-03
  • 概述:滤镜

Android 自定义视频滤镜特效,在拍摄视频时可以用,编写时需要用到第三方类库GPUImage类库,你可以点击文章后面的链接,下载本实例的完整源代码:

/**
 * 显示弹窗
 * @param context 上下文用于创建弹窗
 * @param listener 用于返回设置的滤镜的类型
 */
public static void showDialog(final Context context,
                              final OnFilterChooseListener listener) {
    final FilterList filters = new FilterList();
    filters.addFilter("反相", FilterType.反相);//添加数据
    filters.addFilter("像素", FilterType.像素);//添加数据
    filters.addFilter("复古", FilterType.复古);//添加数据
    filters.addFilter("浮雕", FilterType.浮雕);//添加数据
    filters.addFilter("相片", FilterType.相片);//添加数据
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("选择滤镜的效果");//设置标题
    //设置Item点击事件
    builder.setItems(filters.names.toArray(new String[filters.names.size()]),
           (dialog, which) ->  listener.OnFilterChooseListener(
                   createFilterForType(filters.filters.get(which))));
    builder.create().show();//显示弹窗
}
/**
 * 用于判断弹窗中Item的点击事件
 * @param type
 * @return
 */
private static GPUImageFilter createFilterForType(final FilterType type) {
    switch (type) {
        case 反相:
            return new GPUImageColorInvertFilter();
        case 像素:
            return new GPUImagePixelationFilter();
        case 复古:
            return new GPUImageSepiaFilter();
        case 浮雕:
            return new GPUImageEmbossFilter();
        case 相片:
            return new GPUImagePosterizeFilter();
        default:
            throw new IllegalStateException("没有该类型的滤镜!");
    }
}
/**
 * 回调接口,用于获取滤镜
 */
public interface OnFilterChooseListener {
    void OnFilterChooseListener(GPUImageFilter filter);
}
/**
 * 枚举设置滤镜的类型
 */
private enum FilterType {
    反相, 像素, 复古, 浮雕, 相片,
}
/**
 * 该类用于储存弹窗中将要展示滤镜的信息
 */
private static class FilterList {
    public List<String> names = new LinkedList();//滤镜名称
    public List<FilterType> filters = new LinkedList();//滤镜类型
    /**
     * 设置滤镜名称和滤镜的类型
     * @param name
     * @param filter
     */
    public void addFilter(final String name, final FilterType filter) {
        names.add(name);
        filters.add(filter);
    }
}

主文件代码:

private GPUImage mGPUImage; //用于展示滤镜的效果
private GPUImageFilter mFilter;//滤镜
private Camera camera;//摄像头对象
private int orientation;//图像的显示角度
private int cameraID = Camera.CameraInfo.CAMERA_FACING_BACK;//摄像ID代表前置摄像头或后置摄像头
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取GPUImage对象
    mGPUImage = new GPUImage(this);
    //设置预览
    mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));
    //设置点击事件,用于打开具有滤镜信息的滤镜列表
    findViewById(R.id.button_choose_filter).setOnClickListener(
            v -> SelectFilter.showDialog(MainActivity.this,
                    filter -> switchFilterTo(filter)));
}
@Override
public void permissionSuccess(int requestCode) {
    super.permissionSuccess(requestCode);
    if (requestCode == 1000){
        setCameraPreView();
    }
}
/**
 * 设置滤镜
 *
 * @param filter 用于设置滤镜的效果
 */
private void switchFilterTo(final GPUImageFilter filter) {
    if (mFilter == null
            || (filter != null && !mFilter.getClass().equals(filter.getClass()))) {
        mFilter = filter;//获取滤镜对象
        mGPUImage.setFilter(mFilter);//设置选择的滤镜
    }
}
/**
 * 开启预览
 */
private void setCameraPreView() {
    if (checkCameraHardware()) {
        camera = getCameraInstance();//获取摄像头
        setCameraDisplayOrientation(this, cameraID, camera);//设置摄像角度
        mGPUImage.setUpCamera(camera, orientation, false, false);//设置图像的预览效果
    }
}
/**
 * 检查设备的摄像头是否可用
 *
 * @return 返回true则摄像头可用
 */
private boolean checkCameraHardware() {
    return this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
/**
 * 获取摄像头对象
 *
 * @return
 */
private Camera getCameraInstance() {
    Camera camera = null;
    try {
        camera = Camera.open(cameraID);
    } catch (Exception e) {
    }
    return camera;
}
/**
 * 设置图像的预览方向(官方推荐方法)
 *
 * @param activity
 * @param cameraId
 * @param camera
 */
private void setCameraDisplayOrientation(
        Activity activity, int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
    }
    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;
    } else {
        result = (info.orientation - degrees + 360) % 360;
    }
    this.orientation = result;
    camera.setDisplayOrientation(result);
}
/**
 * 程序运行时开启摄像头
 */
@Override
protected void onResume() {
    super.onResume();
    requestPermission(new String[]{Manifest.permission.CAMERA},1000);
}
/**
 * 界面失去焦点后释放资源
 */
@Override
protected void onPause() {
    super.onPause();
    camera.setPreviewCallback(null);
    camera.release();
    camera = null;
}

    相关声明:

      若“Android为视频拍摄增加复古、浮雕等滤镜效果”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。