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. Cocoapods完整使用篇

    温馨提示:在篇文章中所使用的Xcode版本为Xcode7.   一.什么是CocoaPods? 简单来说,就是专门为iOS工程提供对第三方库的依赖的管理工具,通过CocoaPods,我们可以单独管理每 ...

  2. win7查看端口占用

    1.查看谁占用了我们的80端口,在windows命令行窗口下执行:   netstat -aon|findstr 80 发现80端口被进程号为2596的进程占用.2.查看占用80端口进程的应用程序是什 ...

  3. C市现在要转移一批罪犯到D市,C市有n名罪犯,按照入狱时间有顺序,另外每个罪犯有一个罪行值,值越大罪越重。现在为了方便管理,市长决定转移入狱时间连续的c名犯人,同时要求转移犯人的罪行值之和不超过t,问有多少种选择的方式?

    // ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...

  4. Spark Streaming和Kafka整合开发指南(二)

    在本博客的<Spark Streaming和Kafka整合开发指南(一)>文章中介绍了如何使用基于Receiver的方法使用Spark Streaming从Kafka中接收数据.本文将介绍 ...

  5. 彻底隐藏Nginx版本号的安全性与方法

    Nginx默认是显示版本号的,如: [root@bkjz ~]# curl -I www.nginx.orgHTTP/1.1 200 OKServer: nginx/0.8.44Date: Tue, ...

  6. 深入Asyncio(六)Tasks and Futures

    Tasks and Futures 大多数的工作只涉及到Task.create_task()方法,就像前面代码一样,Future是Task的父类,提供与loop交互的所有功能. Future对象表示某 ...

  7. 【转】php和java之间rsa加密互通

    以下是php封装好的类,引入即可使用 <?php /** * 作者:pjp * 邮箱:vippjp@163.com */ class RSA{ private $privateKey='';// ...

  8. ASP.NET动态网站制作(5)-- 标签语义化及知识补充

    前言:这节课主要是讲标签语义化及一些知识点的补充 内容:参考老师的博文:http://www.cnblogs.com/ruanmou/p/4821894.html

  9. memcached 搭建

     linux 下memcached 的搭建 memcached 下载地址:http://www.danga.com/memcached/dist/ libevent 下载地址:http://libev ...

  10. AWS:5.公有云编程

    主要内容 1.AWS接口 2.使用AWS命令行 3.使用python sdk编程 AWS接口 Console:web控制台 登录amazon后在"我的账户" -> AWS管理 ...