arm-linux-gcc test.c -ljpeg -I /usr/local/libjpeg-8a/include/ -L /usr/local/libjpeg-8a/lib/
这样编译
代码
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<time.h>
#include<fcntl.h>
#include<linux/input.h>
#include<jpeglib.h>
#include<sys/mman.h>
void draw_point(int *p,int x,int y,int color)
{
int *pf = p + (800*y + x);
*pf = color;
}
int main(int argc,char *argv[])
{
int fd=open("/dev/fb0",O_RDWR);
if(-1==fd)
{
perror("open");
return -1;
}
int fd_e = open("/dev/event0",O_RDONLY);
if(-1==fd_e)
{
perror("open_e");
return -1;
}
struct input_event ev;
DIR *dir;
struct dirent *ent;
char str[100][32];
memset(str,0,sizeof(str));
dir=opendir(argv[1]);
if(NULL==dir)
{
perror("opendir");
return;
}
int k=0;
while(ent=readdir(dir))
{
if(ent->d_type==4)
{
continue;
}
strcpy(str[k],ent->d_name);
k++;
}
closedir(dir);
int pricture_len=k;
k=0;
while(1)
{
int x1=0,x2=0,pr1=-1,pr2=-1;
while(1)
{
int r = read(fd_e,&ev,sizeof(ev));
if(r == sizeof(ev))
{
if(ev.type == EV_ABS && ev.code == ABS_X)
{
if(x1==0)
{
x1=ev.value;
}
x2=ev.value;
}
if(ev.type == EV_ABS && ev.code == ABS_PRESSURE)
{
if(pr1==-1)
{
pr1=ev.value;
}
pr2=ev.value;
}
}
if(pr2==0)
{
break;
}
}
if(x1>x2)
{
if(k<pricture_len-1)
{
k++;
}
}
else if(x1<x2)
{
if(k>1)
{
k--;
}
}
char str1[512];
char str2[512];
strcpy(str1,str[k]);
strcpy(str2,str1+strlen(str1)-4);
if(strcmp(str2,".bmp")==0)
{
char data[800*480*4];
printf("%s
",str[k]);
int fd_bmp=open(str[k],O_RDWR);
if(fd_bmp==-1)
{
perror("open bmp");
return ;
}
lseek(fd_bmp,54,SEEK_SET);
read(fd_bmp,data,800*480*4);
lseek(fd,0,SEEK_SET);
int i,j;
for(i=0; i<480; i++)
{
for(j=0; j<800; j++)
{
char r,g,b;
int color;
r=data[((479-i)*800+j)*3];
g=data[((479-i)*800+j)*3+1];
b=data[((479-i)*800+j)*3+2];
color=(b<<16)|(g<<8)|r;
write(fd,&color,4);
}
}
printf("hello
");
close(fd_bmp);
}
else if(strcmp(str2,".jpg")==0)
{
void *addr = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(addr == MAP_FAILED)
{
perror("mmap");
return -1;
}
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
FILE *infile=fopen(str[k],"r");
if(infile == NULL)
{
perror("fopen");
return -1;
}
lseek(fd,0,SEEK_SET);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
unsigned char *buffer = malloc(cinfo.output_width * cinfo.output_components);
while(cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo,&buffer,1);
int x,color;
unsigned char r,g,b;
unsigned char *p = buffer;
for(x = 0; x < 800; x++)
{
r = *p++;
g = *p++;
b = *p++;
color = (r << 16) | (g << 8) | b;
draw_point(addr,x,cinfo.output_scanline-1,color);
}
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
}
}
close(fd);
close(fd_e);
return 0;
}