从相机视频流onPreviewFrame保存图片

转换成bitmap
public class Nv21ToBitmap {

    private Type.Builder yuvType, rgbaType;
    private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
    private RenderScript rs;
    private Allocation in, out;

    public Nv21ToBitmap(Context context){
        rs = RenderScript.create(context);
        yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    }

    public Bitmap nv21ToBitmap( byte[] nv21, int width, int height) {
         if(yuvType == null){
            yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
            in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
            rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
            out = Allocation.createTyped(rs, rgbaType.create(),Allocation.USAGE_SCRIPT);
        }

        in.copyFrom(nv21);
        yuvToRgbIntrinsic.setInput(in);
        yuvToRgbIntrinsic.forEach(out);
        Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        out.copyTo(bmpout);
        return bmpout;
    }
}

根据需求进行图片旋转获取到新的图片

public static Bitmap rotateBitmap(int angle,Bitmap bitmap){
    if(bitmap == null){
        return null;
    }
    Bitmap returnBm = null;
    //根据旋转角度,生成旋转矩阵
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    try {
        //将原始图片按照旋转矩阵进行旋转,并得到新的图片
        returnBm = Bitmap.createBitmap(bitmap,  0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    if(returnBm == null){
        returnBm = bitmap;
    }
    return returnBm;
}

保存在本地path

public static String fileName = "meal_log";
private static String filePath = Environment.getExternalStorageDirectory() + File.separator + fileName + File.separator;
public static void saveBitmapAsPng(Bitmap bitmap,String picName) {
    // 2. 保存Bitmap到文件
    File mediaStorageDir = new File(filePath);
    if (!mediaStorageDir.exists()) {
        mediaStorageDir.mkdirs();
    }
    try (FileOutputStream fos = new FileOutputStream(filePath + picName)) {
        // 3. 将Bitmap压缩为PNG格式并写入文件
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (IOException e) {
        e.printStackTrace();
        Log.d("FileUtils", "saveBitmapAsPng::" + e.toString());
    } finally {
        // 4. 回收Bitmap资源
        bitmap.recycle();
    }
}

示例:

Bitmap bitmap = nv21ToBitmap.nv21ToBitmap(image, mCameraWrapper.getWidth(), mCameraWrapper.getHeight());
Log.e(TAG, "保存图片 bitmap= " + bitmap.getByteCount());

//旋转图片
Bitmap rotateBitmap = FileUtils.rotateBitmap(mRotate, bitmap);
Log.e(TAG, "旋转后图片 bitmap= " + rotateBitmap.getByteCount());

FileUtils.saveBitmapAsPng(rotateBitmap, "palm_detect_test.png");

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值