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的矩阵值是如何存储的? 如何测试我们所实现算法的性能? 查找表是什么?为什么要用它? 测试用例 颜色空间缩减.具体做法就是:将现有颜色空间值除以某个输入 ...
随机推荐
- Nginx与Apache的Rewrite规则的区别
一.Nginx Rewrite规则相关指令 Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx R ...
- spring boot json 首字母大小写问题解决方案
spring boot默认使用的json解析框架是jackson,对于.net转java的项目来说太坑了,首字母大写的属性会自动转为小写,然后前端就悲剧了,十几个属性的ViewModel增加几个Js ...
- WPF的ListView控件自定义布局用法实例
正文: 如何布局是在App.xaml中定义源码如下 <Application x:Class="CWebsSynAssistant.App" xmlns="ht ...
- 认识XmlReader
认识XmlReader 摘要 XmlReader类是组成.NET的关键技术之一,极大地方便了开发人员对Xml的操作.通过本文您将对XmlReader有一个很好的认识,并将其应用到实际开发中. 目录 ...
- cocos2d-x 3.2 for wp8-xaml应用商店提交应用时出现的API错误(不能用CreateEventExA)解决的方法
好不easy做完一个游戏.提交到商店显示"本地API不支持CreateEventExA"之类的错误提示 于是我在整个解决方式里查找CreateEventExA,发现没有,却在Aud ...
- 获得手机的ip
本文转载至 http://blog.csdn.net/showhilllee/article/details/8746114 iosip手机 貌似ASI里获取ip地址的链接不可以了.也曾试过 ...
- Python 单元测试 之setUP() 和 tearDown()
setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对应测试地址,这一些属于前置条件. tearDown:表示释放资源,它在 ...
- poj 2151Check the difficulty of problems<概率DP>
链接:http://poj.org/problem?id=2151 题意:一场比赛有 T 支队伍,共 M 道题, 给出每支队伍能解出各题的概率~ 求 :冠军至少做出 N 题且每队至少做出一题的概率~ ...
- 九度OJ 1150:Counterfeit Dollar(假美元) (分析、检验)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:485 解决:215 题目描述: Sally Jones has a dozen Voyageur silver dollars. Howev ...
- Jaccard Similarity and Shingling
https://www.cs.utah.edu/~jeffp/teaching/cs5955/L4-Jaccard+Shingle.pdf https://www.cs.utah.edu/~jeffp ...