Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have an image in some location (ex:/usr/share/pixmaps/ravi.jpg/png).Now I want to print this image using printer.For this I need store hexadecimal values of the image into unsigned char array.So how to convert image into hexadecimal and store it into unsigned char array.

What I have tried:

BMP preparation,here visb_bmp contains hexadecimal values.For testing purpose statically I gave hexadecimal values to visb_bmp char array.But my requirement is it has to convert image to hex and store it in visb_bmp.Can you please provide solution.

len=prepare_bmp((unsigned char*)bmpbuff,visb_bmp,sizeof(visb_bmp));
ret = prn_write_bmp((unsigned char*)bmpbuff,len);
ret = prn_write_text((unsigned char*)"\n\n\n\n\n",5,1);

int CPrinter::prepare_bmp(unsigned char *bmpbuf,unsigned char *data,int len)
{
int count1,count2;
for(count1=0,count2=0;count1<len;count1++)
{
if(count2 >29800)
{
qDebug("********* %d\n",count2);
return ERROR;
}
if(count1%48==0)
count2=count2+(48*4);

bmpbuf[count2+(47-(count1%48))]=~data[count1];
bmpbuf[count2+48+(47-(count1%48))]=~data[count1];
bmpbuf[count2+96+(47-(count1%48))]=~data[count1];
bmpbuf[count2+48+96+(47-(count1%48))]=~data[count1];
}
return count2;
}
Posted
Updated 9-Apr-18 21:07pm

All data are in fact binary stored as a series of bytes. So there is no need to convert to hex or unsigned char array if you have the data in a buffer because an unsigned char is a byte.

If you want to print a PNG image with Qt use QImage to load the file and paint with QPainter on a QPrinter:
C++
// Use default printer
QPrinter printer(QPrinterInfo::defaultPrinter());

// Or select printer using QPrintDialog
QPrintDialog *dlg = new QPrintDialog(&printer, 0);
int res = dlg->exec();
delete dlg;
if (res != QDialog::Accepted)
    return;

QString fileName = "/usr/share/pixmaps/ravi.jpg/png";
QImage img(fileName);
QPainter painter(&printer);
painter.drawImage(QPoint(0,0), img);
painter.end();

[EDIT]
There is a potentially error in your code regarding the number of bytes to be processed:
C++
int CPrinter::prepare_bmp(unsigned char *bmpbuf,unsigned char *data,int len);

len=prepare_bmp((unsigned char*)bmpbuff,visb_bmp,sizeof(visb_bmp));
When visb_bmp is not an array of fixed size (not unsigned char visb_bmp[VISB_SIZE];) but a pointer to allocated memory, sizeof(visb_bmp) is the size of a pointer which is 4 or 8 depending on the build mode (32 or 64 bit).
[/EDIT]
 
Share this answer
 
v2
Comments
Member 13740197 10-Apr-18 5:45am    
The code is working fine if I take visb_bmp value taken llike below.
So how to convert image to this hexadecimal bytes and save to visb_bmp
unsigned char visb_bmp[]=
{

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x87,0xfd,0xe0,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0x3f,0xe0,0xff,0x81,0xff,0x87,0x7f,0x80,0x7f,0x30, //*16bytes*

0xf8,0x1f,0xfc,0xf,0xf2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x87,0xfc,0xc0,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0x3f,0xe0,0x7f,0x0,0xfe,0x87,0x1f,0x0,0x7c,0x70, //*16bytes*

0xf0,0x1f,0xfc,0xf,0xf9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3,0xfe,0x81,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0x1f,0xe0,0x1f,0x4,0xfc,0x87,0xf,0x0,0x78,0x70, //*16bytes*

0xf0,0x1f,0xfc,0x7,0xf9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x41,0xfe,0x83,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0x1f,0xe0,0xf,0x66,0xf8,0x87,0xf,0x0,0x78,0x70, //*16bytes*

0xf0,0xf,0xfe,0x87,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x21,0xff,0x3,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0xf,0xe0,0xcf,0x64,0xf0,0x87,0x7,0x0,0x78,0x70, //*16bytes*

0xf0,0xf,0xfe,0x3,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x20,0xff,0x7,0x1e, //*16bytes*

0xfc,0xff,0xf0,0x1f,0xfe,0x83,0xf,0xe0,0xc7,0x0,0xe3,0x87,0x7,0x7f,0x70,0xf0, //*16bytes*

0xe0,0xf,0xfe,0x43,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x90,0xff,0xf,0x1e, //*1
};
Jochen Arndt 10-Apr-18 6:04am    
Please don't post such comments with meaningless data.

It is still unclear what you finally want to do.

My solution shows you how to load an image file from disk and print it with Qt according to "Now I want to print this image using printer".
Member 13740197 10-Apr-18 5:46am    
The code you given is not printing anything
Jochen Arndt 10-Apr-18 5:57am    
Probably because the file (which path I have copied from your question) does not exist.
Member 13740197 10-Apr-18 6:06am    
Path I have taken correctly.Is there any way to assign hexadecimal values to visb_bmp char array(as shown above).
As I understand you question you need a png to bmp conversion. Normally you access every pixel and convert it some suiting output data. Read the article CXImage for a deeper understanding of picture manipulation.

A bitmap isnt only a hex dump of picture data but a detailed data format. I think your code is not working and you must first extend your knowledge to understand what you really want to do.
 
Share this answer
 
Comments
Member 13740197 10-Apr-18 5:33am    
The code is working fine if I take visb_bmp value taken llike below.
So how to convert image to this hexadecimal bytes and save to visb_bmp
unsigned char visb_bmp[]=
{

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x87,0xfd,0xe0,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0x3f,0xe0,0xff,0x81,0xff,0x87,0x7f,0x80,0x7f,0x30, //*16bytes*

0xf8,0x1f,0xfc,0xf,0xf2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x87,0xfc,0xc0,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0x3f,0xe0,0x7f,0x0,0xfe,0x87,0x1f,0x0,0x7c,0x70, //*16bytes*

0xf0,0x1f,0xfc,0xf,0xf9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3,0xfe,0x81,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0x1f,0xe0,0x1f,0x4,0xfc,0x87,0xf,0x0,0x78,0x70, //*16bytes*

0xf0,0x1f,0xfc,0x7,0xf9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x41,0xfe,0x83,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0x1f,0xe0,0xf,0x66,0xf8,0x87,0xf,0x0,0x78,0x70, //*16bytes*

0xf0,0xf,0xfe,0x87,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x21,0xff,0x3,0x1f, //*16bytes*

0x3c,0x0,0x70,0x0,0x80,0x83,0xf,0xe0,0xcf,0x64,0xf0,0x87,0x7,0x0,0x78,0x70, //*16bytes*

0xf0,0xf,0xfe,0x3,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x20,0xff,0x7,0x1e, //*16bytes*

0xfc,0xff,0xf0,0x1f,0xfe,0x83,0xf,0xe0,0xc7,0x0,0xe3,0x87,0x7,0x7f,0x70,0xf0, //*16bytes*

0xe0,0xf,0xfe,0x43,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //*16bytes*

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x90,0xff,0xf,0x1e, //*1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900