C++在图片上添加字符串
时间: 2023-12-13 18:03:40 浏览: 85
要在图片上添加字符串,可以使用C++的OpenCV库来实现。下面是一个简单的示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
// 读取图片
Mat img = imread("test.jpg");
// 在图片上添加字符串
putText(img, "Hello, World!", Point(50,50), FONT_HERSHEY_SIMPLEX, 1, Scalar(0,255,0), 2);
// 显示图片
imshow("Image", img);
waitKey(0);
return 0;
}
```
这个程序会在名为`test.jpg`的图片上添加字符串"Hello, World!",并在屏幕上显示结果。`putText()`函数的参数依次为:图片、要添加的字符串、字符串位置、字体、字体大小、颜色和线条宽度。
相关问题
c++ 根据qrcode字符串创建一个子窗口把qrcode生成图片显示在窗口上 不使用qt
你可以使用第三方库 `qrcodegen` 来生成 QR Code 图片,然后再使用 Windows API 中的 GDI+ 库来将图片显示在窗口上。以下是一个示例程序,可以参考一下:
1. 下载 `qrcodegen.hpp` 文件,可以从 [GitHub](https://github.com/nayuki/QR-Code-generator) 上获取。
2. 将 `qrcodegen.hpp` 文件添加到你的项目中。
3. 使用 `qrcodegen` 库生成 QR Code 图片,并将图片显示在窗口上。
```c++
#include <windows.h>
#include <gdiplus.h>
#include "qrcodegen.hpp"
#pragma comment(lib, "Gdiplus.lib")
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 初始化 GDI+
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// 创建窗口
HWND hwnd;
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = L"QRCodeWindow";
if (!RegisterClass(&wc))
return 1;
hwnd = CreateWindow(wc.lpszClassName, L"QR Code", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
if (!hwnd)
return 2;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// 生成 QR Code 图片
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText("Hello, world!", qrcodegen::QrCode::Ecc::LOW);
const int size = qr.getSize();
const int border = 4;
const int imageSize = size * 10 + border * 2;
Gdiplus::Bitmap bitmap(imageSize, imageSize, PixelFormat32bppRGB);
Gdiplus::Graphics graphics(&bitmap);
graphics.Clear(Gdiplus::Color(255, 255, 255, 255));
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (qr.getModule(x, y)) {
const int left = x * 10 + border;
const int top = y * 10 + border;
graphics.FillRectangle(&Gdiplus::SolidBrush(Gdiplus::Color(0, 0, 0)), left, top, 10, 10);
}
}
}
// 进入消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 释放 GDI+
Gdiplus::GdiplusShutdown(gdiplusToken);
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 使用 GDI+ 绘制图片
Gdiplus::Graphics graphics(hdc);
Gdiplus::Bitmap bitmap(L"qrcode.png", FALSE);
graphics.DrawImage(&bitmap, 0, 0);
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
```
这个程序会创建一个窗口,然后在窗口上显示生成的 QR Code 图片。你可以根据需要修改窗口的大小和位置,以及 QR Code 图片的内容和纠错级别。
C++Qt在第一个QlistWidget中选中一个字符串在第一个QlistWidget中显示字符串对应的图片
可以通过QListWidget的信号和槽机制来实现这个功能。
首先,将字符串与对应的图片路径存储在一个QMap中。然后,将字符串添加到QListWidget中,并设置其为可选中。
接下来,连接QListWidget的itemSelectionChanged()信号到一个自定义的槽函数中。在槽函数中,获取当前选中的字符串,从QMap中获取对应的图片路径,并使用QLabel显示图片。
下面是示例代码:
```cpp
// 定义 QMap 存储字符串对应的图片路径
QMap<QString, QString> imageMap;
imageMap["apple"] = "/path/to/apple.png";
imageMap["banana"] = "/path/to/banana.png";
imageMap["orange"] = "/path/to/orange.png";
// 创建 QListWidget
QListWidget *listWidget = new QListWidget(this);
// 添加字符串到 QListWidget
listWidget->addItem("apple");
listWidget->addItem("banana");
listWidget->addItem("orange");
// 设置 QListWidget 可选中
listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
// 连接 itemSelectionChanged() 信号到自定义槽函数
connect(listWidget, &QListWidget::itemSelectionChanged, this, &MainWindow::onItemSelectionChanged);
// 自定义槽函数
void MainWindow::onItemSelectionChanged()
{
// 获取当前选中的字符串
QString selectedString = listWidget->currentItem()->text();
// 获取对应的图片路径
QString imagePath = imageMap.value(selectedString);
// 使用 QLabel 显示图片
QPixmap pixmap(imagePath);
ui->imageLabel->setPixmap(pixmap);
}
```
在上面的示例代码中,我们创建了一个QListWidget,并将字符串添加到其中。然后,设置其为可选中,并连接itemSelectionChanged()信号到一个自定义的槽函数onItemSelectionChanged()中。
在槽函数中,我们获取当前选中的字符串,从QMap中获取对应的图片路径,并使用QLabel显示图片。在这个示例中,我们使用了一个QLabel来显示图片,你可以根据实际需求使用其他控件。