QImage(uchar *data) 如何格式化数组?
QImage(uchar *data) how to format array?
谁能告诉我如何格式化数组以便将其加载到 QImage 中?
现在我有一个二维字符数组:
uchar dataArray[400][400];
time_t t;
srand(time(&t));
int x, y;
for(x=0; x< 400; x++)
{
for(y=0; y<400; y++)
{
dataArray[x][y] = rand()%1001;
}
}
QPainter MyPainter(this);
scene = new QGraphicsScene(this);
scene->addEllipse(200, 200, 20, 20);
ui.graphicsView->setScene(scene);
*image = new QImage(*dataArray, 400, 400, QImage::Format_Mono);
image->setColorCount(2);
image->setColor(1, qRgb(255, 0, 0));//red
image->setColor(0, Qt::transparent);
scene->addPixmap(QPixmap::fromImage(*image));
当数组的内容为 0 时,我想要一种颜色,而另一种颜色的内容 > 0。所以我想将数组加载到单色 QImage 中。显然这个数组是行不通的。数组需要如何格式化才能正确加载我的 QImage?
文档只是说了以下内容,但我真的不明白那是什么意思...
data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
我想要一个 Format_Mono 这样的 QImage 其中 "x" 和“+”代表具有不同颜色(红色和透明)的单个像素:
xxxx+xxx++xxx
xxx++xx++xxxx
++x+x+xxxxxxx
+xxx+x+x+x+xx
为此,我有一个具有相同模式的数组(如上面代码中的 dataArray),其中 x 高于指定值且 + 低于或等于(此时值为 0)。
我如何将此数组转换为 QImage 可以使用 Format_Mono 的数组,以便我可以看到正确的模式?
假设 dataArray
声明为 uchar dataArray[400][400];
QImage tmpImg(dataArray[0], 400, 400, QImage::Format_Grayscale8);
// this image shares memory with dataArray and this is not safe
// for inexperienced developer
QImage result(tmpImg); // shallow copy
result.bits(); // enforce a deep copy of image
找到将我的 dataArray 转换为可用的 imageArray 的解决方案:
一旦你弄清楚它是如何完成的,它就很容易了(显然我不知道为什么我一开始就没有得到它......)。我只需要将每个数据点按位转换为新数组,我还必须弄清楚,imageArray 的轴必须在 [y][x] 顺序中,而不是相反(谁他妈的会按这个顺序做吗?!)。
浪费了很多时间来弄清楚...
uchar dataArray[400][400]; //first index is x-axis, second one is y-axis
uchar imageArray[400][52]; //first index is y-axis, second one is x-axis
time_t t;
srand(time(&t));
int x, y;
for(y=0; y<400; y++)
{
for(x=0; x<400; x++)
{
dataArray[x][y] = rand()%1001;
}
}
for(y=0; y<400; y++)
{
for(x=0; x<400; x++)
{
if(dataArray[x][y] > 0)
imageArray[y][x/8] |= 1 << 7-x%8; //changing bit to 1
//7- because msb is left but we start counting left starting with 0
else
imageArray[y][x/8] &= ~(1 << 7-x%8); //changing bit to 0
}
}
scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);
QImage *image = new QImage(*imageArray, 400, 400, QImage::Format_Mono);
谁能告诉我如何格式化数组以便将其加载到 QImage 中? 现在我有一个二维字符数组:
uchar dataArray[400][400];
time_t t;
srand(time(&t));
int x, y;
for(x=0; x< 400; x++)
{
for(y=0; y<400; y++)
{
dataArray[x][y] = rand()%1001;
}
}
QPainter MyPainter(this);
scene = new QGraphicsScene(this);
scene->addEllipse(200, 200, 20, 20);
ui.graphicsView->setScene(scene);
*image = new QImage(*dataArray, 400, 400, QImage::Format_Mono);
image->setColorCount(2);
image->setColor(1, qRgb(255, 0, 0));//red
image->setColor(0, Qt::transparent);
scene->addPixmap(QPixmap::fromImage(*image));
当数组的内容为 0 时,我想要一种颜色,而另一种颜色的内容 > 0。所以我想将数组加载到单色 QImage 中。显然这个数组是行不通的。数组需要如何格式化才能正确加载我的 QImage? 文档只是说了以下内容,但我真的不明白那是什么意思...
data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
我想要一个 Format_Mono 这样的 QImage 其中 "x" 和“+”代表具有不同颜色(红色和透明)的单个像素:
xxxx+xxx++xxx
xxx++xx++xxxx
++x+x+xxxxxxx
+xxx+x+x+x+xx
为此,我有一个具有相同模式的数组(如上面代码中的 dataArray),其中 x 高于指定值且 + 低于或等于(此时值为 0)。 我如何将此数组转换为 QImage 可以使用 Format_Mono 的数组,以便我可以看到正确的模式?
假设 dataArray
声明为 uchar dataArray[400][400];
QImage tmpImg(dataArray[0], 400, 400, QImage::Format_Grayscale8);
// this image shares memory with dataArray and this is not safe
// for inexperienced developer
QImage result(tmpImg); // shallow copy
result.bits(); // enforce a deep copy of image
找到将我的 dataArray 转换为可用的 imageArray 的解决方案:
一旦你弄清楚它是如何完成的,它就很容易了(显然我不知道为什么我一开始就没有得到它......)。我只需要将每个数据点按位转换为新数组,我还必须弄清楚,imageArray 的轴必须在 [y][x] 顺序中,而不是相反(谁他妈的会按这个顺序做吗?!)。
浪费了很多时间来弄清楚...
uchar dataArray[400][400]; //first index is x-axis, second one is y-axis
uchar imageArray[400][52]; //first index is y-axis, second one is x-axis
time_t t;
srand(time(&t));
int x, y;
for(y=0; y<400; y++)
{
for(x=0; x<400; x++)
{
dataArray[x][y] = rand()%1001;
}
}
for(y=0; y<400; y++)
{
for(x=0; x<400; x++)
{
if(dataArray[x][y] > 0)
imageArray[y][x/8] |= 1 << 7-x%8; //changing bit to 1
//7- because msb is left but we start counting left starting with 0
else
imageArray[y][x/8] &= ~(1 << 7-x%8); //changing bit to 0
}
}
scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);
QImage *image = new QImage(*imageArray, 400, 400, QImage::Format_Mono);