• VC 画图


    图片取模软件工具推荐:Img2Lcd.exe 【支持JPG、BMP、EMF、WBMP、GIF、ICO】

    1. //imgData:RGB888格式数据
    2. //width:图片宽度px
    3. //height:图片高度px
    4. //CMFCApplication1Dlg:自定义的项目MFC对话框类
    5. void Cxxxx::DrawPic(char *imgData,int width,int height)
    6. {
    7. if (imgData == NULL) {
    8. return;
    9. }
    10. CDC MemDC; //首先定义一个显示设备对象
    11. CBitmap MemBitmap;//定义一个位图对象
    12. //随后建立与屏幕显示兼容的内存显示设备
    13. MemDC.CreateCompatibleDC(NULL);
    14. //建立一个与屏幕显示兼容的位图,位图的大小可以用窗口的大小
    15. CRect rcClient;
    16. GetClientRect(&rcClient);
    17. static CDC* pDC = GetDC();
    18. MemBitmap.CreateCompatibleBitmap(pDC, rcClient.Width(), rcClient.Height());
    19. //将位图选入到内存显示设备中
    20. //只有选入了位图的内存显示设备才有地方绘图,画到指定的位图上
    21. CBitmap* pOldBit = MemDC.SelectObject(&MemBitmap);
    22. //先用背景色将位图清除干净.
    23. MemDC.FillSolidRect(0, 0, rcClient.Width(), rcClient.Height(),
    24. RGB(31, 204, 160));//绘图
    25. //start draw data==================
    26. CPoint pt;
    27. COLORREF color = RGB(0, 0, 0);
    28. long index = 0;
    29. for (size_t i = 0; i < height; i++)
    30. {
    31. for (size_t j = 0; j < width; j++)
    32. {
    33. pt.x = j;
    34. pt.y = i;
    35. color = RGB(imgData[index], imgData[index + 1], imgData[index + 2]);
    36. index += 4;//R_G_B_Alpha
    37. MemDC.SetPixel(pt, color);//大图片比较耗时
    38. }
    39. }
    40. //end draw data=================
    41. //将内存中的图拷贝到屏幕上进行显示
    42. pDC->BitBlt(0, 0, rcClient.Width(), rcClient.Height(),
    43. &MemDC, 0, 0, SRCCOPY);
    44. MemBitmap.DeleteObject();
    45. MemDC.DeleteDC();
    46. }
    47. //------------------------------------------
    48. //截取整屏幕保存到g_ScreenData数组
    49. /* Globals */
    50. int g_ScreenX = 0;
    51. int g_ScreenY = 0;
    52. BYTE* g_ScreenData = 0;
    53. void ScreenCap()
    54. {
    55. HDC hScreen = GetDC(NULL);
    56. g_ScreenX = GetDeviceCaps(hScreen, HORZRES);
    57. g_ScreenY = GetDeviceCaps(hScreen, VERTRES);
    58. HDC hdcMem = CreateCompatibleDC(hScreen);
    59. HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, g_ScreenX, g_ScreenY);
    60. HGDIOBJ hOld = SelectObject(hdcMem, hBitmap);
    61. BitBlt(hdcMem, 0, 0, g_ScreenX, g_ScreenY, hScreen, 0, 0, SRCCOPY);
    62. SelectObject(hdcMem, hOld);
    63. BITMAPINFOHEADER bmi = { 0 };
    64. bmi.biSize = sizeof(BITMAPINFOHEADER);
    65. bmi.biPlanes = 1;
    66. bmi.biBitCount = 32;
    67. bmi.biWidth = g_ScreenX;
    68. bmi.biHeight = -g_ScreenY;
    69. bmi.biCompression = BI_RGB;
    70. bmi.biSizeImage = 0;// 3 * ScreenX * ScreenY;
    71. if (g_ScreenData)
    72. free(g_ScreenData);
    73. g_ScreenData = (BYTE*)malloc(4 * g_ScreenX * g_ScreenY);
    74. GetDIBits(hdcMem, hBitmap, 0, g_ScreenY, g_ScreenData, (BITMAPINFO*)&bmi, DIB_RGB_COLORS);
    75. ReleaseDC(GetDesktopWindow(), hScreen);
    76. DeleteDC(hdcMem);
    77. DeleteObject(hBitmap);
    78. }
    79. inline int GetScreenPixelB(int x, int y)
    80. {
    81. return g_ScreenData[4 * ((y * g_ScreenX) + x)];
    82. }
    83. inline int GetScreenPixelG(int x, int y)
    84. {
    85. return g_ScreenData[4 * ((y * g_ScreenX) + x) + 1];
    86. }
    87. inline int GetScreenPixelR(int x, int y)
    88. {
    89. return g_ScreenData[4 * ((y * g_ScreenX) + x) + 2];
    90. }
    91. //------------------------------------
    1. void Cxxxx::DrawBitmap(uint8_t* imgData, int bitCount, int width, int height)
    2. {
    3. #define PC_BITCOUNT 32 //电脑固定32bit图像位数
    4. #define RGB565_RED 0xf800
    5. #define RGB565_GREEN 0x07e0
    6. #define RGB565_BLUE 0x001f
    7. //uint8_t* imgDataOut = (uint8_t*)malloc(width * height * (LV_COLOR_DEPTH / 8));
    8. long pixelCount = width * height;
    9. uint8_t* imgDataOut = (uint8_t*)malloc(pixelCount * 4);//32 BIT 图像
    10. if (bitCount == 16)
    11. {
    12. long i = 0;
    13. long j = 0;
    14. uint8_t r = 0;
    15. uint8_t g = 0;
    16. uint8_t b = 0;
    17. uint16_t n565Color = 0;
    18. do
    19. {
    20. n565Color = (imgData[j] << 8) | imgData[j + 1];
    21. b = (n565Color & RGB565_RED) >> 8;
    22. g = (n565Color & RGB565_GREEN) >> 3;
    23. r = (n565Color & RGB565_BLUE) << 3;
    24. j += 2;
    25. imgDataOut[i + 0] = r;
    26. imgDataOut[i + 1] = g;
    27. imgDataOut[i + 2] = b;
    28. imgDataOut[i + 3] = 0x00;
    29. i += 4;
    30. } while (j < pixelCount * bitCount / 8);
    31. }
    32. else if (bitCount == 24)
    33. {
    34. long i = 0;
    35. long j = 0;
    36. uint8_t r = 0;
    37. uint8_t g = 0;
    38. uint8_t b = 0;
    39. do
    40. {
    41. b = imgData[j + 0];
    42. g = imgData[j + 1];
    43. r = imgData[j + 2];
    44. j += 3;
    45. imgDataOut[i + 0] = r;
    46. imgDataOut[i + 1] = g;
    47. imgDataOut[i + 2] = b;
    48. imgDataOut[i + 3] = 0x00;
    49. i += 4;
    50. } while (j < pixelCount * bitCount / 8);
    51. }
    52. else if (bitCount == 32)
    53. {
    54. memcpy(imgDataOut, imgData, pixelCount * 4);
    55. }
    56. static CDC* pDC = GetDC();
    57. HBITMAP hBitMap = CreateBitmap(width, height, 1, PC_BITCOUNT, imgDataOut);
    58. CBitmap cbmp;
    59. CDC dcMemory;
    60. cbmp.Attach(hBitMap);
    61. dcMemory.CreateCompatibleDC(pDC);
    62. CBitmap* hOldBitmap = dcMemory.SelectObject(&cbmp);
    63. pDC->BitBlt(0, 0, width, height,
    64. &dcMemory,
    65. 0, 0,
    66. SRCCOPY);
    67. cbmp.DeleteObject();
    68. dcMemory.SelectObject(hOldBitmap);
    69. dcMemory.DeleteDC();
    70. #if 0
    71. CString str;
    72. str.Format("DrawData_%dBit",bitCount);
    73. DumpHex("DrawData", imgDataOut, pixelCount * 4);
    74. #endif
    75. free(imgDataOut);
    76. }

    1. void BlockTransfer(int x0,int y0,int imgW,int imgH,uint8_t * imageData)
    2. {
    3. //g_screenBuffer为整个屏幕的buffer,宽度:SCREEN_WIDTH,高度:SCREEN_HEIGH
    4. //源图长宽为sW,sH;在(x0,y0)位置覆盖 imageData 图形数据 长宽 imgW,imgH;
    5. int x = 0;
    6. int y = 0;
    7. int posDest = 0;
    8. int posSrc = 0;
    9. for (int j = 0;j < imgH;j++)
    10. {
    11. y = y0 + j;
    12. if (y > SCREEN_HEIGH)
    13. break;
    14. for (int i = 0; i < imgW; i++)
    15. {
    16. x = x0 + i;
    17. if (x > SCREEN_WIDTH)
    18. break;
    19. posDest = x + SCREEN_WIDTH * y;
    20. posSrc = i + imgH * j;
    21. //请判断坐标是否越界if
    22. //32BIT(4Byte)图处理方式:
    23. g_screenBuffer[4 * posDest + 0] = imageData[4 * posSrc + 0];
    24. g_screenBuffer[4 * posDest + 1] = imageData[4 * posSrc + 1];
    25. g_screenBuffer[4 * posDest + 2] = imageData[4 * posSrc + 2];
    26. g_screenBuffer[4 * posDest + 3] = imageData[4 * posSrc + 3];
    27. }
    28. }
    29. }
    1. void BlockTransfer(int x0,int y0,int imgW,int imgH,uint8_t * imageData,bool bEnableFilter=false, uint32_t filterColor=0)
    2. {
    3. //g_screenBuffer为整个屏幕的buffer,宽度:SCREEN_WIDTH,高度:SCREEN_HEIGH
    4. //源图长宽为sW,sH;在(x0,y0)位置覆盖 imageData 图形数据 长宽 imgW,imgH;
    5. //bEnableFilter 允许过滤色, filterColor 过滤像素的RGB值
    6. int x = 0;
    7. int y = 0;
    8. int posDest = 0;
    9. int posSrc = 0;
    10. uint8_t r, g, b;
    11. uint32_t nColorCmp;
    12. for (int j = 0;j < imgH;j++)
    13. {
    14. y = y0 + j;
    15. if (y > SCREEN_HEIGH)
    16. break;
    17. for (int i = 0; i < imgW; i++)
    18. {
    19. x = x0 + i;
    20. if (x > SCREEN_WIDTH)
    21. break;
    22. posDest = x + SCREEN_WIDTH * y;
    23. posSrc = i + imgH * j;
    24. //请注意判断坐标是否越界
    25. //32BIT(4Byte)图处理方式:
    26. r = imageData[4 * posSrc + 0];
    27. g = imageData[4 * posSrc + 1];
    28. b = imageData[4 * posSrc + 2];
    29. if (bEnableFilter) {
    30. nColorCmp = (r << 16) | (g << 8) | (b);
    31. if (nColorCmp == filterColor) {
    32. continue;
    33. }
    34. }
    35. g_screenBuffer[4 * posDest + 0] = imageData[4 * posSrc + 0];
    36. g_screenBuffer[4 * posDest + 1] = imageData[4 * posSrc + 1];
    37. g_screenBuffer[4 * posDest + 2] = imageData[4 * posSrc + 2];
    38. g_screenBuffer[4 * posDest + 3] = imageData[4 * posSrc + 3];
    39. }
    40. }
    41. }

    1. void qt_draw_bitmap(uint8_t* imgData, int bitCount, uint16_t width, uint16_t height, QPainter* paint)
    2. {
    3. //#include
    4. #define RGB565_RED 0xf800
    5. #define RGB565_GREEN 0x07e0
    6. #define RGB565_BLUE 0x001f
    7. long pixelCount = width * height;
    8. uint8_t* imgDataOut = (uint8_t*)malloc(pixelCount * 4);//32 BIT 图像
    9. if (bitCount == 16)
    10. {
    11. long i = 0;
    12. long j = 0;
    13. uint8_t r = 0;
    14. uint8_t g = 0;
    15. uint8_t b = 0;
    16. uint16_t n565Color = 0;
    17. do
    18. {
    19. n565Color = (imgData[j] << 8) | imgData[j + 1];
    20. b = (n565Color & RGB565_RED) >> 8;
    21. g = (n565Color & RGB565_GREEN) >> 3;
    22. r = (n565Color & RGB565_BLUE) << 3;
    23. j += 2;
    24. imgDataOut[i + 0] = r;
    25. imgDataOut[i + 1] = g;
    26. imgDataOut[i + 2] = b;
    27. imgDataOut[i + 3] = 0x00;
    28. i += 4;
    29. } while (j < pixelCount * bitCount / 8);
    30. }
    31. else if (bitCount == 24)
    32. {
    33. long i = 0;
    34. long j = 0;
    35. uint8_t r = 0;
    36. uint8_t g = 0;
    37. uint8_t b = 0;
    38. do
    39. {
    40. b = imgData[j + 0];
    41. g = imgData[j + 1];
    42. r = imgData[j + 2];
    43. j += 3;
    44. imgDataOut[i + 0] = r;
    45. imgDataOut[i + 1] = g;
    46. imgDataOut[i + 2] = b;
    47. imgDataOut[i + 3] = 0x00;
    48. i += 4;
    49. } while (j < pixelCount * bitCount / 8);
    50. }
    51. else if (bitCount == 32)
    52. {
    53. memcpy(imgDataOut, imgData, pixelCount * 4);
    54. }
    55. QByteArray imageByteArray = QByteArray((const char*)imgDataOut, pixelCount * 4);
    56. uchar* transData = (unsigned char*)imageByteArray.data();
    57. QImage image = QImage(transData, width, height, QImage::Format_RGB32);
    58. free(imgDataOut);
    59. paint->drawImage(QPoint(0, 0), image);//start point=[0,0]
    60. paint->end();
    61. }
    1. void qt_draw_bitmap(uint8_t *imgData16Bit, uint16_t width, uint16_t height, QLabel *label)
    2. {
    3. long pixelCount = width * height;
    4. QByteArray imageByteArray = QByteArray((const char *) imgData16Bit, pixelCount * 2);
    5. uchar *transData = (unsigned char *) imageByteArray.data();
    6. QImage image = QImage(transData, width, height, QImage::Format_RGB16);//RGB565_16bit
    7. QPixmap tmpPixmap = QPixmap::fromImage(image);
    8. label->setPixmap(tmpPixmap);
    9. }
    1. #include <stdio.h>
    2. #include <SDL2/SDL.h>
    3. #define SDL_HOR_RES (800)
    4. #define SDL_VER_RES (600)
    5. typedef struct TagMonitor
    6. {
    7. int screen_w;
    8. int screen_h;
    9. SDL_Renderer *render;
    10. SDL_Texture *texture;
    11. } tagMonitor;
    12. // 将屏幕设置成指定argb颜色
    13. int update_win(tagMonitor *mt, uint32_t argb)
    14. {
    15. uint32_t fb_data[SDL_HOR_RES * SDL_VER_RES]; // frame buffer data[w*h]
    16. for (size_t i = 0; i < SDL_HOR_RES * SDL_VER_RES; i++)
    17. {
    18. fb_data[i] = argb;
    19. }
    20. SDL_UpdateTexture(mt->texture, NULL, fb_data, mt->screen_w * 4);
    21. SDL_RenderClear(mt->render);
    22. // 设定渲染的目标区域
    23. SDL_Rect destRect;
    24. destRect.x = 0;
    25. destRect.y = 0;
    26. destRect.w = mt->screen_w;
    27. destRect.h = mt->screen_h;
    28. // 复制材质到渲染器对象
    29. if (SDL_RenderCopy(mt->render, mt->texture, NULL, &destRect))
    30. {
    31. printf("Error,%s \n", SDL_GetError());
    32. return -1;
    33. }
    34. // 执行渲染操作
    35. SDL_RenderPresent(mt->render);
    36. return 0;
    37. }
    38. int main(void *arg)
    39. {
    40. // 窗口大小
    41. int screen_w = SDL_HOR_RES;
    42. int screen_h = SDL_VER_RES;
    43. // 初始化SDL
    44. if (SDL_Init(SDL_INIT_EVERYTHING))
    45. {
    46. printf("could not initialize SDL: %s\n", SDL_GetError());
    47. return -1;
    48. }
    49. SDL_Window *screen = SDL_CreateWindow("SimpleSDL2", 0, 0, screen_w, screen_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    50. if (!screen)
    51. {
    52. printf("could not create window: %s\n", SDL_GetError());
    53. return -1;
    54. }
    55. SDL_Renderer *render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
    56. if (!render)
    57. {
    58. printf("render create fail! \n");
    59. return -1;
    60. }
    61. SDL_Texture *texture = SDL_CreateTexture(render, // 指定在哪个渲染器当中创建
    62. SDL_PIXELFORMAT_ARGB8888, // 指定当前材质的像素格式
    63. SDL_TEXTUREACCESS_STREAMING, // 设定当前材质可修改
    64. screen_w, screen_h // 指定材质宽高
    65. );
    66. if (!texture)
    67. {
    68. printf("texture create fail! \n");
    69. return -1;
    70. }
    71. tagMonitor mt;
    72. mt.screen_w = screen_w;
    73. mt.screen_h = screen_h;
    74. mt.texture = texture;
    75. mt.render = render;
    76. SDL_Event myEvent; // SDL事件
    77. int quit = 0;
    78. while (!quit) // 建立事件主循环
    79. {
    80. // 注意:事件只针对SDL创建的窗口内有效
    81. while (SDL_PollEvent(&myEvent)) // 从队列里取出事件
    82. {
    83. // printf("event=%d \n", myEvent.type);
    84. switch (myEvent.type) // 根据事件类型分门别类去处理
    85. {
    86. case SDL_QUIT: // 离开事件[点击窗口关闭]
    87. quit = 1; // quit event poll
    88. break;
    89. case SDL_MOUSEMOTION: // 鼠标移动
    90. printf("mouseXY: %d,%d \n", myEvent.motion.x, myEvent.motion.y);
    91. break;
    92. case SDL_MOUSEBUTTONDOWN: // 鼠标点击
    93. printf("ButtonClck:%d\n", myEvent.button.button); // 0:左键,1:中键,2:右键
    94. break;
    95. case SDL_KEYDOWN: // 键盘按下
    96. // 键值表在SDL2/SDL_keycode.h文件中定义
    97. printf("KEY_DOWN:%d\n", myEvent.key.keysym.sym);
    98. break;
    99. case SDL_KEYUP: // 键盘释放
    100. // 键值表在SDL2/SDL_keycode.h文件中定义
    101. printf("KEY_UP:%d\n", myEvent.key.keysym.sym);
    102. break;
    103. }
    104. }
    105. static int cnt = 0;
    106. static int index = 0;
    107. static uint32_t argb = 0xffffffff;
    108. if (cnt++ > 1000)
    109. {
    110. if (index == 0)
    111. argb = 0xFFFF0000; // R
    112. if (index == 1)
    113. argb = 0xFF00FF00; // G
    114. if (index == 2)
    115. argb = 0xFF0000FF; // B
    116. if (index == 3)
    117. argb = 0xFFFFFF00; // Yellow
    118. printf("argb index=%d.\n", index);
    119. cnt = 0;
    120. index = (index + 1) % 4;
    121. }
    122. update_win(&mt, argb);
    123. } //<< while(1)
    124. printf("quit screen_monitor_thread.! \n");
    125. exit(0);
    126. return 0;
    127. }

  • 相关阅读:
    android EventBus
    [Servlet 4]Bean与DAO设计模式
    java毕业生设计校园面包超市系统计算机源码+系统+mysql+调试部署+lw
    护网(HVV)技术详解:网络安全演习的核心技能要求
    【Docker】Docker安装Nginx配置静态资源
    react跳转页面redux数据被清除
    bug场景记录
    树莓派 交叉编译工具链的安装
    集成学习(随机森林)
    MySQL数据库学习【进阶篇】
  • 原文地址:https://blog.csdn.net/wabil/article/details/127955670