如何让开饭板跑出图来


在此之前要在linux安装交叉编译链3.4.5版本
板子文件系统为fs_qtopia.jffs2


首先下载图片开源包libjpeg-turbo-1.2.1.tar.gz
在虚拟机linux下解压包
cd进入解压后的文件夹
然后创建一个_install文件夹用于之后存放配置文件
输入以下命令开始配置
./configure --prefix=/home/book/ARM/tupian/libjpeg-turbo-1.2.1/_install/ --host=arm-linux
然后接着输入
make
make install


这时候你就会发现在你创建的_install底下生成了五个文件夹
bin 、 lib 、include 、man 、share


然后进行代码的编译 代码如下所示:
命令为: arm-linux-gcc jpg2rgb.c lcd_handler.c
 -I/home/book/ARM/tupian/libjpeg-turbo-1.2.1/_install/include  //添加头文件
 -L/home/book/ARM/tupian/libjpeg-turbo-1.2.1/_install/lib     //添加库文件
 -ljpeg -o jpeg   //-ljpeg链接文件
或者直接
 arm-linux-gcc jpg2rgb.c lcd_handler.c
-I/home/book/ARM/tupian/libjpeg-turbo-1.2.1/_install/include
 -ljpeg -o jpeg
编译完成生成jpeg可执行文件


接着我们来连接开发板和虚拟机(用一根网线连接开发板和笔记本)
打开开发板测试开发板和虚拟机是否ping通
如果ping不通把虚拟机桥接模式的自动改成连接本地的网卡
ping通之后输入以下命令挂载到linux虚拟机上
mount -t nfs -o nolock,vers=2 192.168.1.139:/work/nfs_root /mnt
这时候就可以在板子上看到虚拟机/work/nfs_root下的文件
然后把刚刚编译完成的可执行文件jpeg拷到/work/nfs_root文件下
在拷贝一张图片到/work/nfs_root下


然后就可以在板子上输入命令
./jpeg xxx.jpg(图片名称xxx.jpg)


实验完成开发板跑出图片


代码1如下:jpgrgb.c


#include <stdio.h>
#include <setjmp.h>
#include "jpeglib.h" 
#include "lcd_handler.h"


/* Allocate and initialize a JPEG decompression object
//1.分配并初始化一个decompression结构体
Specify the source of the compressed data (eg, a file)
//2.提供一个图片源文件
Call jpeg_read_header() to obtain image info
//3.调用接口解析图片信息

Set parameters for decompression
//4.设置解压参数

jpeg_start_decompress(...);
//5.开始解压

while (scan lines remain to be read)
jpeg_read_scanlines(...);
//6.以scanline为单位读取图片数据

jpeg_finish_decompress(...);
//7.释放这些结构体*/


int main(int argc, char **argv)
{


FILE * infile;
int row_stride;
unsigned char *buffer;


if (argc != 2)
{
printf("Uage: %d <jpg_file>\n",argv[0]);
return -1;
}


FBDeviceInit();
FBCleanScreen(0);


//1.分配并初始化一个decompression结构体
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;


cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);

//2.提供一个图片源文件

if ((infile = fopen(argv[1], "rb")) == NULL) {
   fprintf(stderr, "can't open %s\n", argv[1]);
   return -1;
}
jpeg_stdio_src(&cinfo, infile);


//3.调用接口解析图片信息
jpeg_read_header(&cinfo, TRUE);
printf("width of image is %d\n",cinfo.image_width);
printf("hight of image is %d\n",cinfo.image_height);
printf("num of color :%d\n",cinfo.num_components);


//4.设置解压参数
printf("enter M/N\n");
scanf("%d/%d",&cinfo.scale_num,&cinfo.scale_denom);
printf("scale: %d/%d\n",cinfo.scale_num,cinfo.scale_denom);


//5.开始解压
jpeg_start_decompress(&cinfo);
printf("width of image is %d\n",cinfo.output_width);
printf("hight of image is %d\n",cinfo.output_height);




row_stride = cinfo.output_width * cinfo.output_components;
buffer = (unsigned char *)malloc(row_stride);

//6.以scanline为单位读取图片数据
while (cinfo.output_scanline < cinfo.output_height) 
{
    
    (void) jpeg_read_scanlines(&cinfo, &buffer, 1);
FBShowLine(0, cinfo.image_width, cinfo.output_scanline , buffer);
  }



//7.释放这些结构体*/


jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);


return 0;
}


代码2如下:lcd_handler.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <string.h>
#include <stdlib.h>


#define FB_DEVICE_NAME "/dev/fb0"
#define DBG_PRINTF printf


static int g_fd;


static struct fb_var_screeninfo g_tFBVar;
static struct fb_fix_screeninfo g_tFBFix;
static unsigned char *g_pucFBMem;
static unsigned int g_dwScreenSize;


static unsigned int g_dwLineWidth;
static unsigned int g_dwPixelWidth;


int FBDeviceInit(void)
{
int ret;

g_fd = open(FB_DEVICE_NAME, O_RDWR);
if (0 > g_fd)
{
DBG_PRINTF("can't open %s\n", FB_DEVICE_NAME);
}


ret = ioctl(g_fd, FBIOGET_VSCREENINFO, &g_tFBVar);
if (ret < 0)
{
DBG_PRINTF("can't get fb's var\n");
return -1;
}


ret = ioctl(g_fd, FBIOGET_FSCREENINFO, &g_tFBFix);
if (ret < 0)
{
DBG_PRINTF("can't get fb's fix\n");
return -1;
}

g_dwScreenSize = g_tFBVar.xres * g_tFBVar.yres * g_tFBVar.bits_per_pixel / 8;
g_pucFBMem = (unsigned char *)mmap(NULL , g_dwScreenSize, PROT_READ | PROT_WRITE, MAP_SHARED, g_fd, 0);
if (0 > g_pucFBMem)
{
DBG_PRINTF("can't mmap\n");
return -1;
}


g_dwLineWidth  = g_tFBVar.xres * g_tFBVar.bits_per_pixel / 8;
g_dwPixelWidth = g_tFBVar.bits_per_pixel / 8;

return 0;
}




int FBShowPixel(int iX, int iY, unsigned int dwColor)
{
unsigned char *pucFB;
unsigned short *pwFB16bpp;
unsigned int *pdwFB32bpp;
unsigned short wColor16bpp; /* 565 */
int iRed;
int iGreen;
int iBlue;


if ((iX >= g_tFBVar.xres) || (iY >= g_tFBVar.yres))
{
DBG_PRINTF("out of region\n");
return -1;
}


pucFB      = g_pucFBMem + g_dwLineWidth * iY + g_dwPixelWidth * iX;
pwFB16bpp  = (unsigned short *)pucFB;
pdwFB32bpp = (unsigned int *)pucFB;

switch (g_tFBVar.bits_per_pixel)
{
case 8:
{
*pucFB = (unsigned char)dwColor;
break;
}
case 16:
{
iRed   = (dwColor >> (16+3)) & 0x1f;
iGreen = (dwColor >> (8+2)) & 0x3f;
iBlue  = (dwColor >> 3) & 0x1f;
wColor16bpp = (iRed << 11) | (iGreen << 5) | iBlue;
*pwFB16bpp = wColor16bpp;
break;
}
case 32:
{
*pdwFB32bpp = dwColor;
break;
}
default :
{
DBG_PRINTF("can't support %d bpp\n", g_tFBVar.bits_per_pixel);
return -1;
}
}


return 0;
}


int FBCleanScreen(unsigned int dwBackColor)
{
unsigned char *pucFB;
unsigned short *pwFB16bpp;
unsigned int *pdwFB32bpp;
unsigned short wColor16bpp; /* 565 */
int iRed;
int iGreen;
int iBlue;
int i = 0;


pucFB      = g_pucFBMem;
pwFB16bpp  = (unsigned short *)pucFB;
pdwFB32bpp = (unsigned int *)pucFB;


switch (g_tFBVar.bits_per_pixel)
{
case 8:
{
memset(g_pucFBMem, dwBackColor, g_dwScreenSize);
break;
}
case 16:
{
iRed   = (dwBackColor >> (16+3)) & 0x1f;
iGreen = (dwBackColor >> (8+2)) & 0x3f;
iBlue  = (dwBackColor >> 3) & 0x1f;
wColor16bpp = (iRed << 11) | (iGreen << 5) | iBlue;
while (i < g_dwScreenSize)
{
*pwFB16bpp = wColor16bpp;
pwFB16bpp++;
i += 2;
}
break;
}
case 32:
{
while (i < g_dwScreenSize)
{
*pdwFB32bpp = dwBackColor;
pdwFB32bpp++;
i += 4;
}
break;
}
default :
{
DBG_PRINTF("can't support %d bpp\n", g_tFBVar.bits_per_pixel);
return -1;
}
}


return 0;
}


int FBShowLine(int iXStart, int iXEnd, int iY, unsigned char *pucRGBArray)
{


int i = iXStart * 3;
int iX;
unsigned int dwColor;


if (iY >= g_tFBVar.yres)
return -1;


if (iXStart >= g_tFBVar.xres)
return -1;


if (iXEnd >= g_tFBVar.xres)
{
iXEnd = g_tFBVar.xres;
}

for (iX = iXStart; iX < iXEnd; iX++)
{
/* 0xRRGGBB */
dwColor = (pucRGBArray[i]<<16) + (pucRGBArray[i+1]<<8) + (pucRGBArray[i+2]<<0);
i += 3;
FBShowPixel(iX, iY, dwColor);
}
return 0;
}


代码2头文件: lcd_handler.h
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <string.h>
#include <stdlib.h>




int FBDeviceInit(void);
int FBShowPixel(int iX, int iY, unsigned int dwColor);
int FBCleanScreen(unsigned int dwBackColor);
int FBShowLine(int iXStart, int iXEnd, int iY, unsigned char *pucRGBArray);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值