目前共有5篇帖子。 字體大小:較小 - 100% (默認)▼  內容轉換:港澳繁體▼
 
點擊 回復
634 4
【C語言程序】Nokia5110液晶位圖取碼程序
一派掌門 二十級
1樓 發表于:2016-8-5 17:08
#include <stdio.h>
#include <Windows.h>

int main(void)
{
    BITMAP bmp;
    FILE *fp;
    HBITMAP hbmp;
    HDC hdc, hdcMem;
    int x, y, i;
    unsigned char dat;

    hbmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), TEXT("img.bmp"), IMAGE_BITMAP, (int)NULL, (int)NULL, LR_LOADFROMFILE);
    if (hbmp == NULL)
    {
        puts("打開位圖失敗");
        return 0;
    }

    GetObject(hbmp, sizeof(bmp), &bmp);
    if (bmp.bmWidth == 84 && bmp.bmHeight == 48)
    {
        hdc = GetDC(NULL);
        hdcMem = CreateCompatibleDC(hdc);
        ReleaseDC(NULL, hdc);
        SelectObject(hdcMem, hbmp);

        fopen_s(&fp, "data.c", "w");
        if (fp != NULL)
        {
            fputs("const unsigned char img[][84] = {\n", fp);
            for (y = 0; y < 6; y++)
            {
                fputs("\t{", fp);
                for (x = 0; x < 84; x++)
                {
                    dat = 0;
                    for (i = 0; i < 8; i++)
                    {
                        dat >>= 1;
                        if (GetPixel(hdcMem, x, y * 8 + i) < 0x000010)
                            dat |= 0x80;
                    }
                    if (dat == 0)
                        fputs("0x00", fp);
                    else
                        fprintf(fp, "%#04x", dat);
                    if (x != 83)
                        fputs(", ", fp);
                }
                if (y == 5)
                    fputs("}\n", fp);
                else
                    fputs("},\n", fp);
            }
            fputs("};", fp);
            fclose(fp);
            puts("取碼成功!");
        }
        else
            puts("打開c文件失敗!");
        DeleteDC(hdcMem);
    }
    else
        puts("圖像的尺寸必須為84x48!");
    DeleteObject(hbmp);
    return 0;
}
一派掌門 二十級
2樓 發表于:2016-8-5 17:11
【使用方法】
創建一個Visual Studio工程,類型為Win32控制台程序(空項目),然後新建一個Source.c的源文件,將代碼複製進去後保存。然後把一張尺寸為84x48的bmp位圖放在Source.c所在的文件夾,編譯運行程序即可得到data.c文件,其中的內容就是取碼得到的內容。
 
一派掌門 二十級
3樓 發表于:2016-8-5 17:13
注意:圖像必須是黑白的,尺寸必須為84x48,且格式必須為bmp格式(不能為jpg等其他格式),否則將無法取碼!
 
一派掌門 二十級
4樓 發表于:2016-8-5 17:15
【運行結果】
 
一派掌門 二十級
5樓 發表于:2016-8-5 17:16
【生成的C語言代碼】

 

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:634 回複數:4
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-8-5 17:16
 
©2010-2025 Purasbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。