基于海思开发板的屏幕截图程序(二)

针对 基于海思开发板的屏幕截图程序(一)作了改进,上篇文章的截图结果不正确的原因是:我公司的开发板上fb0中设置的图片格式为ARGB1555,但是我将它作为RGB565来使用,导致转换出来的图片数据不正确,所以压缩后的图片自然会失真。


关键函数是:ARGB1555_to_RGB24() // 将ARGB1555格式的图像数据转换成RGB24的格式。完整代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <fcntl.h>
#include <malloc.h>
#include <linux/fb.h>
#include <jpeglib.h>
#include <jerror.h>
#include <errno.h>extern int errno;/*功能:		获取当前系统时间返回值:	指向时间字符串的开始位置
*/
static const char* getCurTime()					// 获取当前系统时间
{static char ret[30] = {0};time_t t;struct tm *tp;t = time(NULL);tp = localtime(&t);memset(ret, 0, sizeof(ret));sprintf(ret, "%02d%02d%02d_%02d%02d%02d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);return ret;
}/*功能:		RGB565转RGB24函数rgb565:		指向存放rgb565数据的起始地址rgb24:		指向存放rgb24数据的起始地址width:		屏幕(分辨率)的宽度height:		屏幕(分辨率)的高度
*/ 
int RGB565_to_RGB24(unsigned char *rgb565, unsigned char *rgb24, int width, int height)
{int i;int whole = width * height;				// 屏幕像素点个数unsigned char r, g, b;					// 目标缓冲区是RGB格式, 每个分量占1字节, 所以用ucharunsigned short int *pix565;				// 每像素信息占2字节, 所以用short类型pix565 = (unsigned short int *)rgb565;for(i = 0; i < whole; i++){r = ((*pix565) >> 11) & 0x1f;*rgb24 = (r << 3) | (r >> 2);rgb24++;							// 目标像素点后移g = ((*pix565) >> 5) & 0x3f;*rgb24 = (g << 2) | (g >> 4);rgb24++;b = (*pix565) & 0x1f;*rgb24 = (b << 3) | (b >> 2);rgb24++;pix565++;							// 源像素点后移}return 0;
}/*功能:			将ARGB1555格式的图片数据转换成RGB24的图片数据返回值:			0argb1555:[in]	源缓冲区地址, 指向存放argb1555数据的起始地址rgb24:	[out]	目标缓冲区地址, 指向存放rgb24数据的起始地址xres:			屏幕分辨率的宽度yres:			屏幕分辨率的高度
*/
int ARGB1555_to_RGB24(unsigned char *argb1555, unsigned char *rgb24, int xres, int yres)
{int i;int whole = xres * yres;unsigned char r, g, b;unsigned short int *pix1555;pix1555 = (unsigned short int *)argb1555;for(i = 0; i < whole; i++){//	*pix1555 &= 0x7FFFFF;				// 透明分量置为0r = ((*pix1555) >> 10) & 0x1f;		// 取颜色分量: R*rgb24++ = (r << 3) | (r >> 2);g = ((*pix1555) >> 5) & 0x1f;		// 取分量: G*rgb24++ = (g << 3) | (g >> 2);b = (*pix1555) & 0x1f;				// 取分量: B*rgb24++ = (b << 3) | (b >> 2);pix1555++;if(rgb24[-3] == 0xFF && rgb24[-2] == 0 && rgb24[-1] == 0xFF)	// RGB = 0xFF00FFrgb24[-3] = 0;												// 修改RGB = 0x0000FF(将粉红色替换成蓝色)}return 0;
}/*功能:	jpeg压缩函数返回值:	0: 成功, -1: 失败rgb:	指向存放rgb24数据的起始地址width:	屏幕(分辨率)的宽度height:	屏幕(分辨率)的高度
*/
int jpeg_compress(unsigned char *rgb, int width, int height)
{char outfile[100] = {0};struct jpeg_compress_struct cinfo;struct jpeg_error_mgr jerr;FILE * pf = NULL;JSAMPROW row_pointer[1];int row_stride;sprintf(outfile, "snap_%s.jpg", getCurTime());if ((pf = fopen(outfile, "wb")) == NULL){printf("Can not create output file, please check!\n");return -1;}cinfo.err = jpeg_std_error(&jerr);jpeg_create_compress(&cinfo);jpeg_stdio_dest(&cinfo, pf);cinfo.image_width = width;cinfo.image_height = height;cinfo.input_components = 3;				// 1-灰度图,3-彩色图// 输入数据格式为RGBcinfo.in_color_space = JCS_RGB;			// JCS_GRAYSCALE-灰度图,JCS_RGB-彩色图jpeg_set_defaults(&cinfo);jpeg_set_quality(&cinfo, 80, TRUE);		// 设置压缩质量:80jpeg_start_compress(&cinfo, TRUE);		// 开始压缩过程row_stride = width * 3;					// row_stride: 每一行的字节数while (cinfo.next_scanline < cinfo.image_height){row_pointer[0] = &rgb[cinfo.next_scanline * row_stride];(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);}jpeg_finish_compress(&cinfo);			// 完成压缩过程fclose(pf);jpeg_destroy_compress(&cinfo);			// 释放资源return 0;
}int main()
{int fd;struct fb_var_screeninfo fb_var_info;struct fb_fix_screeninfo fb_fix_info;unsigned char *trgb;unsigned char *rgb;int buffer_size;const char *dev = "/dev/fb0";// 打开framebuffer设备fd = open(dev, O_RDONLY);if(fd < 0){printf("fd=%d, error=[%d: %s]\n", fd, errno, strerror(errno));return -1;}// 获取LCD的可变参数ioctl(fd, FBIOGET_VSCREENINFO, &fb_var_info);// 一个像素多少位printf("bits_per_pixel: %d\n", fb_var_info.bits_per_pixel);printf("分辨率: %d x %d\n", fb_var_info.xres, fb_var_info.yres);printf("颜色分量值: (A, R, G, B) = (%d, %d, %d, %d)bits\n", fb_var_info.transp.length, fb_var_info.red.length, fb_var_info.green.length, fb_var_info.blue.length);printf("颜色分量偏移: (A, R, G, B) = (%d, %d, %d, %d)\n", fb_var_info.transp.offset, fb_var_info.red.offset, fb_var_info.green.offset, fb_var_info.blue.offset);// 获取LCD的固定参数ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix_info);// 一帧大小printf("smem_len: %#X\n", fb_fix_info.smem_len);// 一行大小printf("line_length: %#X\n", fb_fix_info.line_length);// 一帧大小buffer_size = (fb_var_info.xres * fb_var_info.yres * fb_var_info.bits_per_pixel / 8);trgb = (unsigned char *)malloc(buffer_size);if(trgb == NULL)exit(0);rgb = (unsigned char *)malloc(fb_var_info.xres * fb_var_info.yres * 3);if(rgb == NULL)goto here;if(read(fd, trgb, buffer_size) < 0)		// 获取一帧数据{printf("read failed!\n");goto read_fail;}//	RGB565_to_RGB24(trgb, rgb, fb_var_info.xres, fb_var_info.yres);			// 将RGB565转换成RGB24格式ARGB1555_to_RGB24(trgb, rgb, fb_var_info.xres, fb_var_info.yres);		// 将ARGB1555转换成RGB24格式if(jpeg_compress(rgb, fb_var_info.xres, fb_var_info.yres) < 0)			// jpeg压缩失败printf("Jpeg compress failed!\n");read_fail:free(rgb);
here:free(trgb);close(fd);return 0;
}

至此,程序运行后得到的图片效果与开发板上所看到的效果完全一样,至少肉眼是分不出差异!


makefile文件如下:

CC=arm-hismall-linux-gcca: a.c$(CC) -o $@ $^ -I/opt/jpeg/include /opt/jpeg/lib/libjpeg.aclean:rm -f *.o a


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/405167.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux 多线程开发-线程的结束pthread_kill和pthread_cancel

1、线程结束的方式 &#xff08;1&#xff09;线程函数中调用pthread_exit函数&#xff0c;不会导致对象析构&#xff0c;可以使用&#xff08;2&#xff09;线程所属的进程结束&#xff0c;进程调用exit&#xff0c;线程C对象不会销毁&#xff0c;不安全&#xff0c;属于被动…

Windows7休眠状态下载技巧攻略

Windows7休眠状态下载技巧攻略 你想让你的快车或者是迅雷通宵下载电影&#xff0c;而且还能省电么?或许你会说&#xff0c;我不关机&#xff0c;给迅雷和快车添加完下载任务&#xff0c;挂在那里不就可以了吗?这没错&#xff0c;开着电脑通宵下载是可以&#xff0c;但这样的方…

支付模块设计

1.背景知识 ssl:SSL为安全套接层&#xff0c;SSL 安全协议最初是由美国网景 Netscape Communication 公司设计开发的&#xff0c;全称为&#xff1a;安全套接层协议 (Secure Sockets Layer) &#xff0c; 它指定了在应用程序协议 ( 如 HTTP 、 Telnet 、 FTP) 和 TCP/IP 之间提…

NB-IOT/Lora/Zigbee/WIFI/蓝牙无线组网方式的对比

NB-IOT/Lora/Zigbee/WIFI/蓝牙无线组网方式的对比 LoRa LoRa(长 距离)是由Semtech公司开发的一种技术&#xff0c;典型工作频率在美国是915MHz&#xff0c;在欧洲是868MHz&#xff0c;在亚洲是433MHz。LoRa的物理层 (PHY)使用了一种独特形式的带前向纠错(FEC)的调频啁啾扩频技…

[react] React必须使用JSX吗?

[react] React必须使用JSX吗&#xff1f; 首先给出答案&#xff0c;不是必须的 只不过通过JSX&#xff0c;你可以简单明了的知道UI是怎样的 JSX is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like 个人简介 …

Qt 设置textEdit插入文本的字体、大小和颜色

1:、打开字体对话框选择字体 //用到头文件&#xff1a;#include <QFontDialog> void MainWindow::on_pushButton_font_clicked() {bool ok;QFont f QFontDialog::getFont(&ok, QFont("Consolas",9), this,"设置显示字体");if(ok){font f;} } …

开发板屏幕截图-适用于本公司海思和智源平台

C源码(screenshot.c)&#xff1a; #include <stdio.h> #include <stdlib.h> #include <sys/ioctl.h> #include <unistd.h> #include <time.h> #include <string.h> #include <fcntl.h> #include <malloc.h> #include <linu…

听小鹏讲废话之OSI

大家好&#xff0c;在下小鹏&#xff0c;大学刚毕业1年&#xff0c;目前从事华为网络设备工作。写这篇文章&#xff0c;有两个目的&#xff0c;第一&#xff0c;和大家分享学习的乐趣&#xff0c;俗话说&#xff0c;没有学不会的学生&#xff0c;只有教不会的老师&#xff1b;第…

python spark 配置

前提&#xff1a;已经装好 java 1.8 和 hadoop 2.7 1. 下载解压放后的目录 /Users/gao/spark-1.4.1-bin-hadoop2.6 2. 在~/.bash_profile 中加入 export PYTHONPATH$PYTHONPATH:/Users/gao/spark-1.4.1-bin-hadoop2.6/python 3. 将/Users/gao/spark-1.4.1-bin-hadoop2.6/python…

Linux du查看磁盘文件夹占用容量

1、du 查看当前文件夹的占用容量&#xff1a; du -sh eg. 查看/usr的占用 2、指定层级查看 查看当前目录下最多一级目录的容量 du -h --max-depth1

Linux下串口参数VTIME和VMIN的用法

VTIME指定了等待的时间,VMIN指定了读取字符的最小数量。 它们不同组合地取值会得到不同的结果&#xff0c;分别如下&#xff1a; 1&#xff0e;当VTIME>0&#xff0c;VMIN>0时。read调用将保持阻塞直到读取到第一个字符&#xff0c;读到了第一个字符之后开始计时&#xf…

[react] 在React中怎么将参数传递给事件?

[react] 在React中怎么将参数传递给事件&#xff1f; 如果使用箭头函数声明函数&#xff0c;调用方式&#xff1a; 不传参&#xff1a;this.func1&#xff0c;如果不传参&#xff0c;事件参数默认会自己添加上 传参&#xff1a; (e) > {this.func1(e,param1, param2)}&…

修改系统默认路径,如收藏夹、桌面、我的文档

修改系统默认路径&#xff0c;如收藏夹、桌面、我的文档。。。。 修改分支为&#xff1a;HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders 上面改的是当前用户的配置噢!

Struts2学习笔记《二》

struts.xml配置文件的全部配置元素&#xff1a;       转载于:https://www.cnblogs.com/abc8023/p/4760284.html

dataset.xsd的定义(vs2008)

定义数据集就是把表拉到.xsd的界面的名称如图&#xff08;http://hi.csdn.net/attachment/201106/9/0_13076420264QqD.gif &#xff09; T_UsersTableAdapter adapter new T_UsersTableAdapter();记得添加引用类型&#xff1b;

[react] 怎样使用Hooks获取服务端数据?

[react] 怎样使用Hooks获取服务端数据&#xff1f; import React, { useState, useEffect } from react; import axios from axios; function App() {const [data, setData] useState({ hits: [] });useEffect(async () > {const result await axios(https://api/url/to/…

Linux mount挂载和umont卸载U盘

1、树莓派插入U盘没有自动挂载&#xff0c;可以在/dev/下看到盘符&#xff0c;但是打不开&#xff0c;需要挂载到指定的文件位置。 2、挂载U盘使用mount命令。 &#xff08;1&#xff09;首先查看插入设备后的文件描述符&#xff1a;找到sda1 (2)新建文件夹 (3)挂载U盘到Udis…

iOS 如果刷新TableViewCell上得数据是空的 添加尾部暂无数据提示

其实很简单 只需要给tableView底部添加一个View 然后View上添加一个Label就可以了 做个衣服判断 数组为空的话则给一个这样的View 转载于:https://www.cnblogs.com/guochaoboke/p/4761429.html

Learning Perl 2

1.子程序 1).创建子程序&#xff1a;使用关键字sub sub marine { print "Hello, world\n"; } calling subroutines&#xff0c;使用&号 &marine (2).返回值&#xff1a;所有的子程序最后一个表达式的运算结果都当作返回值。所以子程序只有“”有用返回值“和…

Ubuntu如何卸载安装的软件以FileZilla为例

1、查看已安装的软件包&#xff1a; dpkg --list 2、卸载FileZilla sudo apt-get remove filezilla