Nv21转Bitmap(高效率转化)

Nv21转Bitmap(高效率转化),第1张

概述原文链接:https://blog.csdn.net/qq1137830424/article/details/81980673转自:https://blog.csdn.net/qq1137830424/article/details/81980673版权声明:本文为博主原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接和本声明。本文链接:https://b 原文链接:https://blog.csdn.net/qq1137830424/article/details/81980673

转自:https://blog.csdn.net/qq1137830424/article/details/81980673

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq1137830424/article/details/81980673
前言
在调Camera的时候有个回调方法onPrevIEwFrame是返回摄像头每一帧的图像数据的,当我们需要对图像数据做处理时就需要Nv21转Bitmap,下面介绍两种方式第一种方式只需要几毫秒时间,第二种方式需要几十毫秒。

第一种方式(高效)
package com.my.camerademo;

import androID.content.Context;
import androID.graphics.Bitmap;
import androID.renderscript.Allocation;
import androID.renderscript.Element;
import androID.renderscript.RenderScript;
import androID.renderscript.ScriptIntrinsicYuvToRGB;
import androID.renderscript.Type;

public class NV21ToBitmap {

    private RenderScript rs;
    private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
    private Type.Builder yuvType, rgbaType;
    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;

    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
这部分代码参考自https://blog.csdn.net/bluegodisplay/article/details/53431798

第二种方式

    private static Bitmap nv21ToBitmap(byte[] nv21, int wIDth, int height) {
        Bitmap bitmap = null;
        try {
            YuvImage image = new YuvImage(nv21, ImageFormat.NV21, wIDth, height, null);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            image.compresstoJpeg(new Rect(0, 0, wIDth, height), 80, stream);
            bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
            stream.close();
        } catch (IOException e) {
            e.printstacktrace();
        }
        return bitmap;
    }
————————————————
版权声明:本文为CSDN博主「许柏胜」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq1137830424/article/details/81980673

总结

以上是内存溢出为你收集整理的Nv21转Bitmap(高效率转化)全部内容,希望文章能够帮你解决Nv21转Bitmap(高效率转化)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1102174.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存