如何在循环中保存具有不同名称的图像文件(.jpg)? [英] How to save image files (.jpg) with different names within a loop?
问题描述
您好,
我正在尝试在C ++循环中保存具有不同名称的文件,我发现了许多解决方案,但不幸的是它没有不适合我的工作。
我的功能将QRcode保存为图像(.jpg)。执行保存的语句的语法如下:
Hello,
I'm trying to save files with different names within a loop in C++, I've found a number of solutions, but unfortunately it doesn't fit my work.
My function saves a QRcode as an image (.jpg). the syntax of the statement which does the saving is as follows:
ERRCODE _stdcall BCSaveImageA ( t_BarCode * pBarCode,
LPCSTR lpszFileName,
e_IMType eImageType,
LONG lXSize,
LONG lYSize,
DOUBLE dXRes,
DOUBLE dYRes
)
其中:
where:
Parameters:
[in] pBarCode Pointer to barcode structure.
[in] lpszFileName Filename
[in] eImageType Enumeration for the type of the image.
[in] lXSize X-Size of the image [pixel]. For Vector-EPS the unit is [0.001 mms]
[in] lYSize Y-Size of the image [pixel]. For Vector-EPS the unit is [0.001 mms]
[in] dXRes X-Resolution of the image [pixels / inch].
[in] dYRes Y-Resolution of the image [pixels / inch].
成功用于在程序中生成单个文件,如下所示:
which is used successfully to generate a single file in my program as follows:
eCode = BCSaveImageA(pBC,"C://webcontent/QR12.jpg", 4, LBarcodeWidth,LBarcodeHieght, dpi, dpi);
问题是如何保存具有不同名称的多个图像文件(.jpg)?我使用了for循环并尝试在上面的语句中写下新文件的名称,如下所示:
The problem is how can I save a number of image files (.jpg) with different names? I've used a for loop and tried to write the name of the new file in the above statement as follows:
eCode = BCSaveImageA(pBC,"C://webcontent/QR"+IntToStr(i)+".jpg", 4, LBarcodeWidth,LBarcodeHieght, dpi, dpi);
其中IntToStr是一个将int转换为字符串的函数
where IntToStr is a function that converts int to string
string IntToStr(int n)
{
stringstream result;
result << n;
return result.str();
}
谢谢你&最好的问候。
Rania
Thank you & Best Regards.
Rania
推荐答案
循环看起来没问题,但看不到其他代码。
一个错误是文件名需要LPCSTR这是一个C风格的字符串,所以转换你需要的C ++字符串
the loop looks OK, but can't see other code.
one mistake is the filename needs LPCSTR which is a C style string so to convert the C++ string you'd need
str.c_str()
第二个错误是串联+使用字符串而不是C字符串。
所以一个想法是适应方法:
second mistake is concatenation with + works with string but not C strings.
so one idea is to adapt method:
string IntToStr(int n)
{
stringstream result;
result<< "C://webcontent/QR"<<n<<".jpg";
return result.str();
}
然后将字符串转换为
then convert string to
LPCSTR
您的问题是关于构建LPCSTR。
尝试以下
Your question is really about constructing a LPCSTR.
Try the following
#include<string>
using namespace std;
for(int n = 0; n < 20; n++)
{
string filename = "C://webcontent/QR"+ to_string((_ULonglong)n) +".jpg";
eCode = BCSaveImageA(pBC, filename.c_str(), 4, LBarcodeWidth,LBarcodeHieght, dpi, dpi);
}
如果您的编译器符合C ++ 11标准,则不需要_ULonglong强制转换。
If your compiler is C++11 compliant you wont need the _ULonglong cast.
这篇关于如何在循环中保存具有不同名称的图像文件(.jpg)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!