如何在HTML中显示QR码图像? [英] How to display QR code image in HTML?

查看:70
本文介绍了如何在HTML中显示QR码图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用通气管构建一个嵌入式网络服务器,它从客户端接收某种数据,以生成QR码。

QR码(保存为图像)必须显示在客户端才能被客户端捕获。

问题是我无法显示它!

我在嵌入式服务器中使用了HTML代码,如下所示:



 <   html  >  
< body >
< span class =code-keyword>< img src = QR。 jpg >
< / img >
< / body > ;
< / html >





我得到的是一个里面装有X的小盒子。

这是我的C ++代码:



  #pragma hdrstop 
#pragma argsused

#include < snorkel.h >
#include < tchar.h >
# include < stdio.h >
#include < stdlib.h >

call_status_t index_htm(snorkel_obj_t http, / * 从此对象读取环境* /
snorkel_obj_t outstream) / * 将数据写入输出流* /
{
if (snorkel_printf(outstream, < html>< body>< img src = QR4.jpg alt = QRcode width = 178 height = 178> // width = 100%height = 100%alt = QRcode>// alt = QRcode width = 178 height = 178>
< / img>< / body>< / html> \\\\ n)== SNORKEL_ERROR)
返回 HTTP_ERROR;

return HTTP_SUCCESS;
}

void 语法( char * pszProg)
{
fprintf(stderr, 语法错误:\ n);
fprintf(stderr, %s [-p< port>] \ n,pszProg);
退出( 1 );
}

void main( int argc, char * argv [])
{
int i = 1 ;
int port = 8888 ;
snorkel_obj_t http = 0 ;
char szExit [ 10 ];
for (; i< argc; i ++)
{
if (argv [i] [ 0 ] == ' - ' || argv [i] [ 0 ] == ' /'
{
char carg = argv [i] [ 1 ];
switch (carg)
{
case ' p' / * 端口号* /
port = atoi(argv [i + 1 ]);
i ++;
break ;
默认
语法(argv [ 0 ]);
break ;
}
}
}

/ *
*
*初始化API
*
* /

如果(snorkel_init()! = SNORKEL_SUCCESS)
{
perror( 无法初始化snorkel \ n);
// exit(1);
}
/ *
*
*创建服务器对象
*
* /

http = snorkel_obj_create(snorkel_obj_server, 5 / * 要创建的处理程序线程数* /
NULL / * 包含的目录index.html * / );
// 或者我可以输入/ Users / rania / Documents / images
if (!http)
{
perror( 无法创建http server \\\
);
// exit(1);
}

/ *
*
*创建一个监听器
*
* /

if (snorkel_obj_set(http, / * 服务器对象* /
snorkel_attrib_listener, / * attribute * /
port, / * 端口号* /
0 / * SSL支持* /
!= SNORKEL_SUCCESS)
{
fprintf(stderr, 无法创建监听器\ n );
snorkel_obj_destroy(http);
// exit(1);
}

/ *
*
*重载网址index.html
*
* /

// 这个生成没有QR码的空盒子
// ================== ==========================
if ( snorkel_obj_set(http, // 服务器对象
snorkel_attrib_uri, // 属性类型
GET, // 方法
/ index.html // url
encodingtype_text,index_htm)!= SNORKEL_SUCCESS)

{
perror( 无法重载index.html);
snorkel_obj_destroy(http);
// exit(1);
}
if (snorkel_obj_set(http,snorkel_attrib_ipvers,IPVERS_IPV4,SOCK_SET)!= SNORKEL_SUCCESS)
{
fprintf(stderr, 错误无法设置ip version \\\
);
// exit(1);
}

fprintf(stderr, \ n \ n [HTTP]启动嵌入式服务器\ n);
if (snorkel_obj_start(http)!= SNORKEL_SUCCESS)
{
perror( 无法启动server \\\
);
snorkel_obj_destroy(http);
// exit(1);
}

/ *
*
*在服务器运行
*时执行某项操作单独的线程
*
* /

fprintf(stderr, \ n [HTTP] started.\\\
\ n

- 按Enter键终止 - \ n);
fgets(szExit, sizeof (szExit),stdin);

fprintf(stderr, [HTTP] bye \ n) ;

/ *
*
*优雅清理
*
* /

snorkel_obj_destroy(http);
退出( 0 );
}







我刚读过这个以HTML格式显示图片,我们必须将图像转换为DATA URL(BASE64),并且有很多在线转换器站点可以做到这一点。但问题是我的项目不使用静态QR码,它会为每个客户生成不同的QR码。此外,我的图像大小比这些在线转换器显示的图像大。



那么,有什么办法可以用C ++以编程方式将这些图像转换成DATA URI(BASE64)吗?

或者有什么方法可以在不转换的情况下显示我的二维码?



几个月来我一直在努力解决这个问题现在,请你需要帮助!



最好的问候。

Rania

解决方案

http://stackoverflow.com/questions / 21400254 / how-to-draw-a-qr-code-with-qt-in-native-cc [ ^ ]

请查看以上讨论,这可以帮到你。

谢谢。


我看到你的index.html文件中的index_htm函数服务器。我没有看到呼吸管代码来提供QR.jpg文件。你需要一个类似qr_jpeg的功能才能将图像文件发送到浏览器。


你好,



我是刚刚更改第12行:

 snorkel_printf(outstream,<   html  >  <  正文 >  <   img     src   = < span class =code-keyword> file:///webcontent/QR4.jpg > 
< / img > < / body > < / html > \r\\\
);


Hi,

I've build an embedded web-server using snorkel, which receives some sort of data from the client, in order to generate a QR code.
the QR code (which is saved as an image)must be displayed on the client-side to be captured by the client.
The problem is I cant display it!
I've used the HTML code within the embedded server as follow:

<html>
<body>
<img src = QR.jpg> 
</img>
</body>
</html>



All I get is a small box with an X inside of it.
And this is my C++ code:

#pragma hdrstop
#pragma argsused

#include <snorkel.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>

call_status_t index_htm(snorkel_obj_t http,     /*read environment from this object */
			snorkel_obj_t outstream)  /* write data to the output stream   */
  {
   if (snorkel_printf (outstream, "<html><body><img src=QR4.jpg alt=QRcode width=178 height=178>" //width=100% height=100% alt=QRcode>" // alt=QRcode width=178 height=178>"
								   "</img></body></html>\r\n")== SNORKEL_ERROR)
		 return HTTP_ERROR;

   return HTTP_SUCCESS;
   }

void syntax (char *pszProg)
	{
	 fprintf (stderr, "syntax error:\n");
	 fprintf (stderr, "%s [-p <port>]\n", pszProg);
	 exit (1);
	 }

void main (int argc, char *argv[])
 {
  int i = 1;
  int port = 8888;
  snorkel_obj_t http = 0;
  char szExit[10];
  for (; i < argc; i++)
  {
   if (argv[i][0] == '-' || argv[i][0] == '/')
	 {
	  char carg = argv[i][1];
	  switch (carg)
	   {
		case 'p':          /* port number */
		port = atoi (argv[i + 1]);
		i++;
		break;
		default:
		syntax (argv[0]);
		break;
		}
	  }
   }

/*
 *
 * initialize API
 *
 */
 if (snorkel_init () != SNORKEL_SUCCESS)
  {
	perror ("could not initialize snorkel\n");
	//exit (1);
   }
/*
 *
 * create a server object
 *
 */
 http = snorkel_obj_create (snorkel_obj_server, 5,     /* number of handler threads to create */
								 NULL       /* directory containing index.html     */);
 //or i can put "/Users/rania/Documents/images"
  if (!http)
   {
	 perror ("could not create http server\n");
	 //exit (1);
   }

/*
 *
 * create a listener
 *
 */
 if (snorkel_obj_set (http,    /* server object */
					  snorkel_attrib_listener, /* attribute   */
					  port,    /* port number */
					  0 /* SSL support */ )
					  != SNORKEL_SUCCESS)
	  {
	   fprintf (stderr, "could not create listener\n");
	   snorkel_obj_destroy (http);
	   //exit (1);
	  }

 /*
  *
  * overload the URL index.html
  *
  */
	 //THIS GENERATES AN EMPTY BOX WITH NO QR CODE
	 //============================================
 if (snorkel_obj_set (http,    // server object
					 snorkel_attrib_uri,      // attribute type
					 GET,     // method
					 "/index.html",   // url
					 encodingtype_text, index_htm) != SNORKEL_SUCCESS)

		 {
		   perror ("could not overload index.html");
		   snorkel_obj_destroy (http);
		   //exit (1);
		  }
if (snorkel_obj_set(http, snorkel_attrib_ipvers, IPVERS_IPV4, SOCK_SET) != SNORKEL_SUCCESS)
		{
		  fprintf (stderr, "error could not set ip version\n");
		  //exit (1);
		 }

 fprintf (stderr, "\n\n[HTTP] starting embedded server\n");
  if (snorkel_obj_start (http) != SNORKEL_SUCCESS)
   {
	perror ("could not start server\n");
	snorkel_obj_destroy (http);
	//exit (1);
	}

/*
 *
 * do something while server runs
 * as a separate thread
 *
 */
  fprintf (stderr, "\n[HTTP] started.\n\n"
					 "--hit enter to terminate--\n");
  fgets (szExit, sizeof (szExit), stdin);

  fprintf (stderr, "[HTTP] bye\n");

 /*
  *
  * graceful clean up
  *
  */
  snorkel_obj_destroy (http);
  exit (0);
  }




And I've just read that to display an image in HTML, we must convert the image into DATA URL (BASE64), and there are a lot of online converter sites that does that. But the problem is that my project does not use a static QR code, it generates different QR codes with each client. Plus, the size of my image is way larger than the images displayed by these online converters.

So, is there any way I can convert these images into DATA URI (BASE64) programmatically using C++?
Or is there any way I can display my QR code without converting it?

I've been struggling with this problem for few month now, Please need help!

Best Regards.
Rania

解决方案

http://stackoverflow.com/questions/21400254/how-to-draw-a-qr-code-with-qt-in-native-c-c[^]
Please check the above discussion, may this help you.
Thanks.


I see the index_htm function server your index.html file. I don't see snorkel code to serve the QR.jpg file. You need a similar function like "qr_jpeg" to send the image file to the browser.


Hello,

I've just changed line 12 with the following:

snorkel_printf (outstream,"<html><body><img src="file:///webcontent/QR4.jpg">"
"</img></body></html>\r\n");


这篇关于如何在HTML中显示QR码图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆