javacpp-ffmpeg系列:

javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片

javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转换为YUV、BGR24或RGB24等图像像素数据

javacpp-FFmpeg系列之3: 图像数据转换(BGR与BufferdImage互转,RGB与BufferdImage互转)

补充:

javacpp-FFmpeg系列补充:FFmpeg解决avformat_find_stream_info检索时间过长问题

前言

ffmpeg获取的数据一般为yuv,argb,rgb,bgr,abgr等图像像素数据,我们可能需要转换为java的图像,来方便我们显示他们,当然不需要转换也可以达到我们的目的。

在线演示demo:https://blog.csdn.net/eguid_1/article/details/82842904

项目维护地址:https://github.com/eguid/easyCV

一、那么先来个RGB像素使用的小demo压压惊

(密集恐惧症预警)

通过这个RGB像素的小demo,更容易理解RGB像素格式

public static int getRGB(int[] rgbarr) {//RGB24数组转整型RGB24

        int rgb = ((int) rgbarr[0]) & 0xff | (((int) rgbarr[1]) & 0xff) << 8 | (((int) rgbarr[2]) & 0xff) << 16

                | 0xff000000;

        return rgb;

    }

    private static int createRandomRgb() {//随机生成RGB24

        int[] rgbarr = new int[3];

        rgbarr[0] = (int) (Math.random() * 255);

        rgbarr[1] = (int) (Math.random() * 255);

        rgbarr[2] = (int) (Math.random() * 255);

        return getRGB(rgbarr);

    }

//eguid原创文章,转载请注明出处和作者名(blog.csdn.net/eguid_1)

   public static void main(String[] args){

        int width = 800, height = 600;

//使用整型RGB24

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        for (int i = 0; i < width; i++) {

            for (int j = 0; j < height; j++) {

                int rgb = createRandomRgb();

                image.setRGB(i, j, rgb);

            }

        }

        JLabel label = new JLabel();

        label.setSize(width, height);

        label.setIcon(new ImageIcon(image));

JFrame frame = new JFrame();

        frame.setSize(width, height);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(label);

        frame.setVisible(true);

        Timer timer=new Timer("定时刷新", true);

        timer.scheduleAtFixedRate(new TimerTask() {

            @Override

            public void run() {

                for (int i = 0; i < width; i++) {

                    for (int j = 0; j < height; j++) {

                        int rgb =createRandomRgb();

                        image.setRGB(i, j, rgb);

                    }

                }

                label.repaint();

            }

        }, 100, 1000/25);

    }

运行结果:


RGB像素格式demo截图-eguid

二、像素图像数据转换为java图像

上一章我们已经通过ffmpeg获取到了AVFrame,要是能预览这帧视频图像那岂不是更好?

要完成转换为java的图像数据这个转换我们分为两步转换流程。

1、像素图像转换为ByteBuffer

不管是yuv,还是rgb或是bgr,都可以使用该函数转换为ByteBuffer,不同的是ByteBuffer转换为BufferedImage的异同。

//eguid原创文章,转载请注明出处和作者名(https://blog.csdn.net/eguid_1)

public ByteBuffer saveFrame(AVFrame pFrame, int width, int height){

        BytePointer data = pFrame.data(0);

        int size = width * height * 3;

        ByteBuffer buf = data.position(0).limit(size).asBuffer();

        return buf;

    }

2、ByteBuffer转换为BufferImage

由于不同像素数据转换方法都不相同,以RGB和BGR为例:

(1)BGR24转BufferedImage

/**

     * 24位BGR转BufferedImage

     * @param src -源数据

     * @param width -宽度

     * @param height-高度

     * @return

     */

    public static BufferedImage BGR2BufferedImage(ByteBuffer src,int width,int height) {

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);

        Raster ra = image.getRaster();

        DataBuffer out = ra.getDataBuffer();

        DataBufferByte db=(DataBufferByte)out;

        ByteBuffer.wrap(db.getData()).put(src);

        return image;

    }

/**

     * 24位整型BGR转BufferedImage

     * @param src -源数据

     * @param width -宽度

     * @param height-高度

     * @return

     */

    public static  BufferedImage BGR2BufferedImage(IntBuffer src,int width,int height) {

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);

        Raster ra = image.getRaster();

        DataBuffer out = ra.getDataBuffer();

        DataBufferInt db=(DataBufferInt)out;

        IntBuffer.wrap(db.getData()).put(src);

        return image;

    }

(2)RGB24转BufferedImage

/**

     * 24位整型RGB转BufferedImage

     * @param src -源数据

     * @param width -宽度

     * @param height-高度

     * @return

     */

    public static  BufferedImage RGB2BufferedImage(IntBuffer src,int width,int height) {

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Raster ra = image.getRaster();

        DataBuffer out = ra.getDataBuffer();

        DataBufferInt db=(DataBufferInt)out;

        IntBuffer.wrap(db.getData()).put(src);

        return image;

    }

三、一个方便预览BufferImage的小函数

/**

     * 使用窗口显示BufferedImage图片

     * @param image -BufferedImage

     */

    public static void viewImage(BufferedImage image) {

        int width=image.getWidth(),height=image.getHeight();

        JLabel label = new JLabel();

        label.setSize(width, height);

        label.setIcon(new ImageIcon(image));

JFrame frame = new JFrame();

        frame.setSize(width, height);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(label);

        frame.setVisible(true);

    }

四、补充

1、BufferImage转Base64编码,方便网页显示

/**

     * bufferedImage转base64

     * @param format -格式(jpg,png,bmp,gif,jpeg等等)

     * @return

     * @throws IOException

     */

    public static String bufferedImage2Base64(BufferedImage image, String format) throws IOException {

        Encoder encoder = Base64.getEncoder();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();// 字节流

        ImageIO.write(image, format, baos);// 写出到字节流

        byte[] bytes=baos.toByteArray();

        // 编码成base64

        String jpg_base64 = encoder.encodeToString(bytes);

        return jpg_base64;

    }

2、保存成图片文件

//eguid原创文章,转载请注明出处和作者名(blog.csdn.net/eguid_1)

public static void saveImage(BufferedImage image,String format,String file) throws IOException {

        ImageIO.write(image, format, new File(file));

    }

javacpp-FFmpeg系列之3: 像素图像数据转换(BGR与BufferdImage互转,RGB与BufferdImage互转,BufferdImage转Base64编码)的更多相关文章

  1. Caffe框架,图像数据转换成LMDB数据格式

    小码农最近在研究深度学习,对所学知识做点记录,以供以后翻阅.在Caffe框架中,数据的格式都是LMDB的,如何将图像数据转换成这个格式呢? 首先,将图像数据和标签生成txt文档,执行一下代码: fin ...

  2. ffmpeg 系列博客

    https://www.ffmpeg.org/download.html#build-macffmpeg 系列博文https://me.csdn.net/blog/leixiaohua1020http ...

  3. BASE64编码和解码(VC源代码) 并 内存加载 CImage 图像

      BASE64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本.完整的BASE64定义可见 RFC1421和 RFC2045.编码后的数据比原始数据略长,为原来的4/3.在电子 ...

  4. OpenCV中图像的BGR格式及Img对象的属性说明

    1. 图像的BGR格式说明 OpenCV中图像读入的数据格式是numpy的ndarray数据格式.是BGR格式,取值范围是[0,255]. 如下图所示,分为三个维度: 第一维度:Height 高度,对 ...

  5. rust实战系列-base64编码

    前言 某些只能使用ASCII字符的场景,往往需要传输非ASCII字符的数据,这时就需要一种编码可以将数据转换成ASCII字符,而base64编码就是其中一种. 编码原理很简单,将原始数据以3字节(24 ...

  6. caffe学习系列(1):图像数据转换成db(leveldb/lmdb)文件

    参考:http://www.cnblogs.com/denny402/p/5082341.html 上述博文用caffe自带的两张图片为例,将图片转为db格式.博主对命令参数进行了详细的解释,很赞. ...

  7. Caffe学习系列(11):图像数据转换成db(leveldb/lmdb)文件

    在深度学习的实际应用中,我们经常用到的原始数据是图片文件,如jpg,jpeg,png,tif等格式的,而且有可能图片的大小还不一致.而在caffe中经常使用的数据类型是lmdb或leveldb,因此就 ...

  8. Java版流媒体编解码和图像处理(JavaCPP+FFmpeg)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. OpenCV从入门到放弃系列之——如何扫描图像、利用查找表和计时

    目的 如何遍历图像中的每一个像素? OpenCV的矩阵值是如何存储的? 如何测试我们所实现算法的性能? 查找表是什么?为什么要用它? 测试用例 颜色空间缩减.具体做法就是:将现有颜色空间值除以某个输入 ...

随机推荐

  1. dnSpy进行反编译修改并编译运行EXE或DLL

    dnSpy对目标程序(EXE或DLL)进行反编译修改并编译运行 本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 本文使用的工具下载地址为: h ...

  2. C#里类的get和set方法编写和调用

    using System; class Date { int day; int month; int year; public int Day{ get { return day; } set { d ...

  3. PHP与js之间的交互

    <?php//在php中药想使用jquery,首先需要导入jquery类库 echo "<script src='".base_url('static')." ...

  4. python 基础 5.0 python类一般形式

    一. 类的一般形式 创建类我们一般使用class 关键字来创建一个类,class 后面跟类型名字,可以自定义,最后以冒号结尾,如下所示:   #/usr/bin/python #coding=utf- ...

  5. 【BZOJ4514】[Sdoi2016]数字配对 费用流

    [BZOJ4514][Sdoi2016]数字配对 Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ...

  6. about gnu bash shell

    1 定义字符串不需要引号 var=NONE echo $var ==>NONE 2 支持基本的整数计算 a=1 b=2 echo $((a+b)) ==>3 必须用$(()),双括号的形式 ...

  7. 操作符表示指针指向的底层值 切片 nill 清空 按值引用赋值 获取地址赋值

    package main import "fmt" var thisVisitedUrls [] string func tf() { p := &thisVisitedU ...

  8. Flask:工厂函数和蓝本

    我们用pycharm去新建Flask项目的时候,会默认生成开发文件.如下,其中包括static,templates,flask1_prj.py文件 在最初开始的时候,我们的app等声明都是在flask ...

  9. mongoDB多级子文档查询

    db.getCollection('product').find({'coverage':{'$elemMatch':{'plan':{'$elemMatch':{'iscoverage':{'$in ...

  10. Cortex-M3 Bit-Banding

    Cortex-M3 Bit-Banding 1. 概述 CM3的存储器系统支持所谓的"位带"(bit-band)操作. 通过它,实现了对单一bit的原子操作.位带操作仅适用于一些特 ...