javacpp-FFmpeg系列之3: 像素图像数据转换(BGR与BufferdImage互转,RGB与BufferdImage互转,BufferdImage转Base64编码)
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);
}
运行结果:
二、像素图像数据转换为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编码)的更多相关文章
- Caffe框架,图像数据转换成LMDB数据格式
小码农最近在研究深度学习,对所学知识做点记录,以供以后翻阅.在Caffe框架中,数据的格式都是LMDB的,如何将图像数据转换成这个格式呢? 首先,将图像数据和标签生成txt文档,执行一下代码: fin ...
- ffmpeg 系列博客
https://www.ffmpeg.org/download.html#build-macffmpeg 系列博文https://me.csdn.net/blog/leixiaohua1020http ...
- BASE64编码和解码(VC源代码) 并 内存加载 CImage 图像
BASE64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本.完整的BASE64定义可见 RFC1421和 RFC2045.编码后的数据比原始数据略长,为原来的4/3.在电子 ...
- OpenCV中图像的BGR格式及Img对象的属性说明
1. 图像的BGR格式说明 OpenCV中图像读入的数据格式是numpy的ndarray数据格式.是BGR格式,取值范围是[0,255]. 如下图所示,分为三个维度: 第一维度:Height 高度,对 ...
- rust实战系列-base64编码
前言 某些只能使用ASCII字符的场景,往往需要传输非ASCII字符的数据,这时就需要一种编码可以将数据转换成ASCII字符,而base64编码就是其中一种. 编码原理很简单,将原始数据以3字节(24 ...
- caffe学习系列(1):图像数据转换成db(leveldb/lmdb)文件
参考:http://www.cnblogs.com/denny402/p/5082341.html 上述博文用caffe自带的两张图片为例,将图片转为db格式.博主对命令参数进行了详细的解释,很赞. ...
- Caffe学习系列(11):图像数据转换成db(leveldb/lmdb)文件
在深度学习的实际应用中,我们经常用到的原始数据是图片文件,如jpg,jpeg,png,tif等格式的,而且有可能图片的大小还不一致.而在caffe中经常使用的数据类型是lmdb或leveldb,因此就 ...
- Java版流媒体编解码和图像处理(JavaCPP+FFmpeg)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- OpenCV从入门到放弃系列之——如何扫描图像、利用查找表和计时
目的 如何遍历图像中的每一个像素? OpenCV的矩阵值是如何存储的? 如何测试我们所实现算法的性能? 查找表是什么?为什么要用它? 测试用例 颜色空间缩减.具体做法就是:将现有颜色空间值除以某个输入 ...
随机推荐
- apt-get update --> Bad header line (fresh install) Ign http://archive.ubuntu.com natty-security/multiverse Sources/DiffIndex W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/natty/Rele
apt-get update --> Bad header line (fresh install) fresh natty install i386 desktop. I get this e ...
- Chrome自带恐龙小游戏的源码研究(三)
在上一篇<Chrome自带恐龙小游戏的源码研究(二)>中实现了云朵的绘制和移动,这一篇主要研究如何让游戏实现昼夜交替. 昼夜交替的效果主要是通过样式来完成,但改变样式的时机则由脚本控制. ...
- request 获取请求头
/********************************************************servlet页面********************************** ...
- ios -- 极光推送《1》
昨天公司项目要加入远程推送功能,自己做显然会很麻烦,所以用了极光的远程推送,下面我会讲如何制作推送证书并使用极光推送进行远程推送. 先讲讲怎么下载推送证书吧(前面的很简单要是知道的可以直接往下滑,简书 ...
- python 基础 6.2 raise 关键字使用
一. raise 关键字 raise 用来触发异常 语法如下: raise[Exception [,args [,traceback]]] 语句中Exception 是异常 ...
- Windows操作系统远程Linux服务器传输文件方法(以EasyDSS云平台、EasyNVR上传部署为例)
本文转自博客:https://blog.csdn.net/black_3717/article/details/79769406 问题背景: 之前给客户部署我们一款EasyDSS云平台(配合EasyN ...
- 【分享】自己写的一个可空的DateTimePicker控件-附源码
最近这段时间在重构以前的一个项目,其中有一项就是要把DateTimePicker控件值可空.大家都知道的DateTimePicker值为DateTime类型,DateTime类型值不能等于Null.但 ...
- iOS 转场动画核心内容
CATransition——转场动画 CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点. ...
- (转)扫盲--JavaScript的立即执行函数
看过jQuery源码的人应该知道,jQuery开篇用的就是立即执行函数.立即执行函数常用于第三方库,好处在于隔离作用域,任何一个第三方库都会存在大量的变量和函数,为了避免变量污染(命名冲突),开发者们 ...
- 【python】使用python发送文本内容邮件
下面提供了一个使用python做的发送文本内容的邮件代码,能够在邮件内容中设置文字颜色,大小,换行等功能. #auther by zls #_*_coding:utf-8_*_ import sys ...