温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Android中实现摄像头高斯模糊

发布时间:2021-04-09 17:24:15 来源:亿速云 阅读:180 作者:Leah 栏目:移动开发

怎么在Android中实现摄像头高斯模糊?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

核心代码:

/**
 * 转换数据并进行模糊处理
 */
public Bitmap blur(byte[] data, Camera camera,float blurvaule){
  Camera.Size previewSize = camera.getParameters().getPreviewSize();
  if (yuvType == null)
  {
    yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length);
    in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

    rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(previewSize.width).setY(previewSize.height);
    out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
  }

  in.copyFrom(data);

  yuvToRgbIntrinsic.setInput(in);
  yuvToRgbIntrinsic.forEach(out);

  Bitmap bmpout = Bitmap.createBitmap(previewSize.width, previewSize.height, Bitmap.Config.ARGB_8888);
  out.copyTo(bmpout);
  //return adjustPhotoRotation(blurBitmap(bmpout,blurvaule),90);
  return blurBitmap(bmpout,blurvaule);
}


/**
 * 模糊处理Bitmap
 * @param bitmap
 * @return
 */
private Bitmap blurBitmap(Bitmap bitmap,float vaule) {
  // 用需要创建高斯模糊bitmap创建一个空的bitmap
  Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
  // 初始化Renderscript,这个类提供了RenderScript context,
  // 在创建其他RS类之前必须要先创建这个类,他控制RenderScript的初始化,资源管理,释放
  // 创建高斯模糊对象

  // 创建Allocations,此类是将数据传递给RenderScript内核的主要方法,
  // 并制定一个后备类型存储给定类型
  Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
  Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
  // 设定模糊度
  blurScript.setRadius(vaule);
  // Perform the Renderscript
  blurScript.setInput(allIn);
  blurScript.forEach(allOut);
  // Copy the final bitmap created by the out Allocation to the outBitmap
  allOut.copyTo(outBitmap);
  // recycle the original bitmap
  bitmap.recycle();
  // After finishing everything, we destroy the Renderscript.
  rs.destroy();
  return outBitmap;
}

ok,这两个方法就够了,将返回的Bitmap给ImageView就可以了,之前一直以为是用JNI实现的,试了一下才发现JAVA也可以,效果也不错,网上也没类似教程就写出来给需要的人。对了,还需要在项目的build.gradle中加入

defaultConfig {
  .......

  renderscriptTargetApi 21
  renderscriptSupportModeEnabled true
}

关于怎么在Android中实现摄像头高斯模糊问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI