在我們IT行業(yè)每天面對的就是敲代碼,所以很多人無法接受這份工作,因為很無聊也很枯燥,長期工作會使人情緒低落,其實我們編程很多時候也有有趣的地方,接下來我就用一個簡單的c語言作圖來緩解一下氣氛。
新的一年開始了,是時候作出改變了。
以下為用C語言畫心形的三種方式(附代碼)
畫心1
關于%*.*s
小數點.后“*”表示輸出位數,具體的數據來自參數表
printf格式字符串中,與寬度控制和精度控制有關的常量都可以換成變量,方法就是使用一個“*”代替那個常量,然后在后面提供變量給“*”。
同樣,小數點.前也可以添加*,也要用戶輸入一個位寬值來代替,表示輸出的字符所占位寬。
也就是說,前面定義輸出總寬度,后面定義輸出字符個數。
printf("%*.*s\n", 50, 3, a); // 50表示此次輸出占位寬,
//3表示輸出a數組的三個字符
畫心2
畫心3
Linux上通過framebuffer將jpeg圖片畫在屏幕上
安裝JPEG庫
1.解壓jpeg源碼 tar -xzvf jpegsrc.v8a.tar.gz
2.在/home/xxx下新建jpeg目錄 mkdir jpeg
3.進入jpeg源碼目錄jpeg-8a cd jpeg-8a
4.生成makefile腳本 ./configure --prefix=/home/xxx/jpeg
5.編譯 make
6.安裝 make install
安裝完畢后就可以在/home/xxx/jpeg目錄下看到 jpeg解碼庫
配置JPEG庫
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include "jpeglib.h"
typedef struct Tag_RGB
{
unsigned char ucRed;
unsigned char ucGreen;
unsigned char ucBlue;
}St_RGB;
int main (int agrc, char *argv[])
{
int fp=0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
fp = open("/dev/fb0",O_RDWR);
if (fp < 0)
{
printf("Error : Can not open framebuffer device\n");
exit(EXIT_FAILURE);
}
if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo))
{
printf("Error reading fixed information\n");
exit(EXIT_FAILURE);
}
if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo))
{
printf("Error reading variable information\n");
exit(EXIT_FAILURE);
}
printf("The mem is :%d\n", finfo.smem_len);
printf("The line_length is :%d\n", finfo.line_length);
printf("The xres is :%d\n", vinfo.xres);
printf("The yres is :%d\n", vinfo.yres);
printf("bits_per_pixel is :%d\n", vinfo.bits_per_pixel);
unsigned long screensize = 0;
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
//這就是把fp所指的文件中從開始到screensize大小的內容給映射出來,
//得到一個指向這塊空間的指針
unsigned char *fbp =(unsigned char *)mmap(0, screensize, PROT_READ|PROT_WRITE
, MAP_SHARED, fp, 0);
if (fbp == (unsigned char*)-1)
{
printf ("Error: failed to map framebuffer device to memory./n");
exit (EXIT_FAILURE);
}
unsigned int location = 0;
struct jpeg_decompress_struct jinfo;
struct jpeg_error_mgr jerr;
/*Bind error handler*/
jinfo.err = jpeg_std_error(&jerr);
/*init jpeg decompress object*/
jpeg_create_decompress(&jinfo);
/*Bind jpg data object*/
FILE * pfJPG = fopen(argv[1], "rb");
if(NULL == pfJPG)
{
printf("open %s failed. error->%s\n", argv[1], strerror(errno));
jpeg_destroy_decompress(&jinfo);
}
else
{
jpeg_stdio_src(&jinfo, pfJPG);
jpeg_read_header(&jinfo, TRUE);
/*Start decompressing*/
jpeg_start_decompress(&jinfo);
/*Only get decompress arguments*/
//jpeg_calc_output_dimensions(&jinfo);
printf("Output Width = %d\n",jinfo.output_width);
printf("Output Height = %d\n",jinfo.output_height);
//Color Channel
printf("Output Components = %d\n",jinfo.output_components);
unsigned char** ppucRowData = NULL;
ppucRowData = (*jinfo.mem->alloc_sarray)((j_common_ptr) &jinfo, JPOOL_IMAGE
, jinfo.output_width * jinfo.output_components, 1);
unsigned int i = 0;
unsigned int j = 0;
St_RGB stColor = {0};
while (jinfo.output_scanline < jinfo.output_height) //jinfo.output_height
{
jpeg_read_scanlines(&jinfo, ppucRowData, 1);
for (i=0; i<jinfo.output_width; i++)
{
location = i*(vinfo.bits_per_pixel/8)+j*finfo.line_length;
stColor.ucRed = ppucRowData[0][i*jinfo.output_components];
stColor.ucGreen = ppucRowData[0][i*jinfo.output_components+1];
stColor.ucBlue = ppucRowData[0][i*jinfo.output_components+2];
/*直接賦值來改變屏幕上某點的顏色*/
*(fbp + location) = stColor.ucBlue;
*(fbp + location + 1) = stColor.ucGreen;
*(fbp + location + 2) = stColor.ucRed;
*(fbp + location + 3) = 0; /*是否透明*/
}
j++;
}
/*Finish Decompress*/
jpeg_finish_decompress(&jinfo);
/*Destroy Decompress Object*/
jpeg_destroy_decompress(&jinfo);
fclose(pfJPG);
}
munmap (fbp, screensize); /*解除映射*/
close (fp);
return 0;
}
編譯:gcc heart.c -o ourheart -ljpeg
運行:./ourheart
轉載請注明來自夕逆IT,本文標題:《c語言編程心形代碼(如何用 C 語言畫心形)》

還沒有評論,來說兩句吧...