把从framebuffer抓出来的数据转换成jpg图像
来源:互联网 发布:淘宝刷流量 猎流 编辑:程序博客网 时间:2024/04/30 04:59
把从framebuffer抓出来的数据转换成为jpg图象
2008-07-10 14:15
//gcc -o grab grab.c -ljpeg
//The source code of grab.c
//cat /dev/fb/0 >tmp
//./grab -i tmp -o grab.jpg
#include <setjmp.h>
#include <stdio.h>
#include <getopt.h>
#include <jpeglib.h>
#define W 800
#define H 480
/*The name of this program.*/
const char *program_name;
/*
Prints usage information for this program to STREAM,and exit the program with EXIT_CODE.
*/
void print_usage(FILE* stream,int exit_code)
{
fprintf(stream,"Usage: %s options [inputfile outputfile...]\n",program_name);
fprintf(stream,
" -h --help Display this usage information\n"
" -i --input filename Read from file\n"
" -o --output filename Write output to file\n"
);
exit(exit_code);
}
void convert_line( char *src, char *dst, int line,int weight,int height );
int main(int argc,char **argv)
{
int next_option;
unsigned int image_height = 0;
unsigned int image_width = 0;
const char* const short_opti;
const struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"input",1, NULL, 'i'},
{"output",1, NULL, 'o'},
{NULL, 0, NULL,0 }
};
const char *output_filename = NULL;
char *input_filename = NULL;
program_name = argv[0];
do{
next_option = getopt_long(argc,argv,short_options,long_options,NULL);
switch(next_option)
{
case 'h':
print_usage(stdout,0);
case 'o':
output_filename = optarg;
break;
case 'i':
input_filename = optarg;
break;
case '?':
print_usage(stdout,1);
case -1:
break;
default:
abort();
}
}
while(next_option != -1);
if(!output_filename){
output_filename = "grab.jpg";
}
if(!input_filename)
{
input_filename = "tmp";
}
printf("outputfile = %s,inputfile = %s\n",output_filename,input_filename);
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPLE *buffer;
JSAMPLE *image_buffer;
buffer = malloc(W*H*3);
image_buffer = malloc(W*3);
FILE *in;
int ret = 0;
in = fopen(input_filename,"r");
if(!in){
printf("please provide the rgb source\n");
exit(0);
}
ret = fread(buffer, 1,W*H*2, in );
if( ret == 0 ){
printf("read image buffer error\n");
exit(0);
}
int height = H;
int width = W;
FILE *outfile;
JSAMPROW row_pointer[1];//points to JSAMPLE row[s]
//setp 1:allocate and initialize JPEG compression object
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
//step 2:specify data destination
//char *filename="grab1.jpg";
if((outfile = fopen(output_filename,"wb")) == NULL){
printf("cannot open file to write\n");
exit(0);
}
jpeg_stdio_dest( &cinfo, outfile );
//step 3:set parameters for compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 100, TRUE );
//step 4:start compresor
jpeg_start_compress(&cinfo, TRUE);
//step 5:
while(cinfo.next_scanline < cinfo.image_height ){
convert_line( buffer,image_buffer,cinfo.next_scanline,height,width);
//printf("%d======end",cinfo.next_scanline);
row_pointer[0] = image_buffer;
(void)jpeg_write_scanlines( &cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose( outfile );
jpeg_destroy_compress(&cinfo);
free(buffer);
free(image_buffer);
}
void convert_line(char* src,char* dst,int line,int width,int height){
int NUM1 = W*2;
int NUM2 = W*3;
char *p = src + line*NUM1;
int i = 0;
while( i < NUM2 ){
dst[i] = (p[1]&0xf8 );
dst[i+1] = ((p[1]&0x7)<<5) | ((p[0] & 0xe0)>>3);
dst[i+2] = (p[0]&0x1f)<<3;
i += 3;
p += 2;
}
}
//The source code of grab.c
//cat /dev/fb/0 >tmp
//./grab -i tmp -o grab.jpg
#include <setjmp.h>
#include <stdio.h>
#include <getopt.h>
#include <jpeglib.h>
#define W 800
#define H 480
/*The name of this program.*/
const char *program_name;
/*
Prints usage information for this program to STREAM,and exit the program with EXIT_CODE.
*/
void print_usage(FILE* stream,int exit_code)
{
fprintf(stream,"Usage: %s options [inputfile outputfile...]\n",program_name);
fprintf(stream,
" -h --help Display this usage information\n"
" -i --input filename Read from file\n"
" -o --output filename Write output to file\n"
);
exit(exit_code);
}
void convert_line( char *src, char *dst, int line,int weight,int height );
int main(int argc,char **argv)
{
int next_option;
unsigned int image_height = 0;
unsigned int image_width = 0;
const char* const short_opti;
const struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"input",1, NULL, 'i'},
{"output",1, NULL, 'o'},
{NULL, 0, NULL,0 }
};
const char *output_filename = NULL;
char *input_filename = NULL;
program_name = argv[0];
do{
next_option = getopt_long(argc,argv,short_options,long_options,NULL);
switch(next_option)
{
case 'h':
print_usage(stdout,0);
case 'o':
output_filename = optarg;
break;
case 'i':
input_filename = optarg;
break;
case '?':
print_usage(stdout,1);
case -1:
break;
default:
abort();
}
}
while(next_option != -1);
if(!output_filename){
output_filename = "grab.jpg";
}
if(!input_filename)
{
input_filename = "tmp";
}
printf("outputfile = %s,inputfile = %s\n",output_filename,input_filename);
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPLE *buffer;
JSAMPLE *image_buffer;
buffer = malloc(W*H*3);
image_buffer = malloc(W*3);
FILE *in;
int ret = 0;
in = fopen(input_filename,"r");
if(!in){
printf("please provide the rgb source\n");
exit(0);
}
ret = fread(buffer, 1,W*H*2, in );
if( ret == 0 ){
printf("read image buffer error\n");
exit(0);
}
int height = H;
int width = W;
FILE *outfile;
JSAMPROW row_pointer[1];//points to JSAMPLE row[s]
//setp 1:allocate and initialize JPEG compression object
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
//step 2:specify data destination
//char *filename="grab1.jpg";
if((outfile = fopen(output_filename,"wb")) == NULL){
printf("cannot open file to write\n");
exit(0);
}
jpeg_stdio_dest( &cinfo, outfile );
//step 3:set parameters for compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 100, TRUE );
//step 4:start compresor
jpeg_start_compress(&cinfo, TRUE);
//step 5:
while(cinfo.next_scanline < cinfo.image_height ){
convert_line( buffer,image_buffer,cinfo.next_scanline,height,width);
//printf("%d======end",cinfo.next_scanline);
row_pointer[0] = image_buffer;
(void)jpeg_write_scanlines( &cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose( outfile );
jpeg_destroy_compress(&cinfo);
free(buffer);
free(image_buffer);
}
void convert_line(char* src,char* dst,int line,int width,int height){
int NUM1 = W*2;
int NUM2 = W*3;
char *p = src + line*NUM1;
int i = 0;
while( i < NUM2 ){
dst[i] = (p[1]&0xf8 );
dst[i+1] = ((p[1]&0x7)<<5) | ((p[0] & 0xe0)>>3);
dst[i+2] = (p[0]&0x1f)<<3;
i += 3;
p += 2;
}
}
- 把从framebuffer抓出来的数据转换成jpg图像
- 把从framebuffer抓出来的数据转换成jpg图像
- MFC 从内存中读了jpg图像显示出来
- 把JPG图片转换成PDF格式的格式转换器
- 怎么把JPG格式的图片转换成PDF
- 怎么把JPG格式的图片转换成PDF
- 把JPG转换成PDF的实用方法
- 快速批量把jpg转换成pdf的方法
- 使用FFmpeg把视频转换成JPG格式的图片
- 图像转换:JPG转换成bmp
- 怎样把jpg图片转换成pdf
- 在线把jpg转换成pdf方法
- 怎样才能把jpg格式转换成pdf
- 怎样才能把pdf文件转换成jpg
- 将jpg图像转换成yuv420p格式
- 使用Window的DC把OpenCV读入的图像数据绘制出来
- 使用openCV把视频转换成一组图像或者从一组图像转换成视频
- 怎么把从mysql数据库中去取出来的数据放到xml文件
- Windows 7 下虚拟串口环境
- 解决IE9下Extjs4文字显示太小问题
- 一个门外汉的产品设计漫谈
- 定时每天备份mysql并定时删除上月记录(脚本)
- Windows8 udp广播局域网聊天程序
- 把从framebuffer抓出来的数据转换成jpg图像
- android学习笔记5 android按键事件处理代码走读[framework]
- C/C++关键字static,const,inline,define,typedef
- mysql中采用concat来拼接中文字符乱码解决方式
- android学习笔记6 eventhub
- Web service misc
- 关于自动化测试
- java参数传递
- ExtJS简单的动画效果