Edit the QR code online and send it to the thermal printer for printing

Source: Internet
Author: User
Edit the QR code online and send it to the thermal printer for printing
Online Editing

Js plug-ins used:

Qrcodesvg

Draw the QR code plugin colorPicker based on the information

Used for color selection, js binding event changes the color of the QR code (svg) canvg

It is used to convert a two-dimensional code in svg format to an html5 canvas, and then generate the base64 encoded data of the two-dimensional code image using the toDataURL method, and send the two-dimensional code to the backend thermal printer through Ajax to print the two-dimensional code.

Convert the image to BMP format. Class for converting images to BMP:

  1. /**
  2. * Class jpg, gif, png ========> BMP
  3. *
  4. * {Description :-
  5. * Class that resize and convert jpg, gif or png to bmp
  6. *}
  7. * For more info contact with me (mahabub1212@yahoo.com)
  8. * You can modify or use or redistribute this class.
  9. */
  10. Class ToBmp {
  11. // New image width
  12. Var $ new_width;
  13. // New image height
  14. Var $ new_height;
  15. // Image resources
  16. Var $ image_resource;
  17. Function image_info ($ source_image ){
  18. $ Img_info = getimagesize ($ source_image );
  19. Switch ($ img_info ['Mime ']) {
  20. Case "image/jpeg": {$ this-> image_resource = imagecreatefromjpeg ($ source_image); break ;}
  21. Case "image/gif": {$ this-> image_resource = imagecreatefromgif ($ source_image); break ;}
  22. Case "image/png": {$ this-> image_resource = imagecreatefrompng ($ source_image); break ;}
  23. Default: {die ("image Error ");}
  24. }
  25. }
  26. Public function imagebmp ($ file_path = ''){
  27. If (! $ This-> image_resource) die ("image Error ");
  28. $ Picture_width = imagesx ($ this-> image_resource );
  29. $ Picture_height = imagesy ($ this-> image_resource );
  30. If (! Imageistruecolor ($ this-> image_resource )){
  31. $ Tmp_img_reource = imagecreatetruecolor ($ picture_width, $ picture_height );
  32. Imagecopy ($ tmp_img_reource, $ this-> image_resource, 0, 0, 0, 0, $ picture_width, $ picture_height );
  33. Imagedestroy ($ this-> image_resource );
  34. $ This-> image_resource = $ tmp_img_reource;
  35. }
  36. If (int) $ this-> new_width> 0 & (int) $ this-> new_height> 0 ){
  37. $ Image_resized = imagecreatetruecolor ($ this-> new_width, $ this-> new_height );
  38. Imagecopyresampled ($ image_resized, $ this-> image_resource, 0, 0, 0, $ this-> new_width, $ this-> new_height, $ picture_width, $ picture_height );
  39. Imagedestroy ($ this-> image_resource );
  40. $ This-> image_resource = $ image_resized;
  41. }
  42. $ Result = '';
  43. $ BiBPLine = (int) $ this-> new_width> 0 & (int) $ this-> new_height> 0 )? $ This-> new_width * 3: $ picture_width * 3;
  44. $ BiStride = ($ biBPLine + 3 )&~ 3;
  45. $ BiSizeImage = (int) $ this-> new_width> 0 & (int) $ this-> new_height> 0 )? $ BiStride * $ this-> new_height: $ biStride * $ picture_height;
  46. $ BfOffBits = 54;
  47. $ BfSize = $ bfOffBits + $ biSizeImage;
  48. $ Result. = substr ('bm ', 0, 2 );
  49. $ Result. = pack ('vvvv ', $ bfSize, 0, 0, $ bfOffBits );
  50. $ Result. = (int) $ this-> new_width> 0 & (int) $ this-> new_height> 0 )? Pack ('vvvvvvvvv ', 40, $ this-> new_width, $ this-> new_height, 1, 24, 0, $ biSizeImage, 0, 0, 0, 0 ): pack ('vvvvvvvvv ', 40, $ picture_width, $ picture_height, 1, 24, 0, $ biSizeImage, 0, 0, 0, 0 );
  51. $ Numpad = $ biStride-$ biBPLine;
  52. $ H = (int) $ this-> new_width> 0 & (int) $ this-> new_height> 0 )? $ This-> new_height: $ picture_height;
  53. $ W = (int) $ this-> new_width> 0 & (int) $ this-> new_height> 0 )? $ This-> new_width: $ picture_width;
  54. For ($ y = $ h-1; $ y> = 0; -- $ y ){
  55. For ($ x = 0; $ x <$ w; ++ $ x ){
  56. $ Col = imagecolorat ($ this-> image_resource, $ x, $ y );
  57. $ Result. = substr (pack ('v', $ col), 0, 3 );
  58. }
  59. For ($ I = 0; $ I <$ numpad; ++ $ I ){
  60. $ Result. = pack ('C', 0 );
  61. }
  62. }
  63. If ($ file_path = ''){
  64. Header ("Content-type: image/bmp ");
  65. Echo $ result;
  66. } Else {
  67. $ Fp = fopen ($ file_path, "wb ");
  68. Fwrite ($ fp, $ result );
  69. Fclose ($ fp );
  70. // ====================
  71. }
  72. Return;
  73. }
  74. }

Usage

  1. $ ToBMP = new ToBmp ();
  2. $ ToBMP-> image_info ($ path_to_img );
  3. $ ToBMP-> new_width = 255;
  4. $ ToBMP-> fig = 255;
  5. $ Output_path = realpath(PATH.'test.bmp ');
  6. $ ToBMP-> imagebmp ($ output_path );

The BMP format varies with the data structure based on the file header information.

I have 24-bit BMP. after 54 bytes are removed from the header file, each three bytes (RGB) represent a vertex.

After merging RGB (three bytes into one byte), the binarization pixel (1 bit, that is, the point is black or white. The reason for binarization is that my thermal printer prints black and white without printing color ).

Each 8-bit is spliced into 1 byte and expressed in hexadecimal format of '\ xx. Data can be printed after being sent to the printer based on the interface provided by the printer.

Note:

1. Use of unpack

  1. $ Content = file_get_contents ($ path_to_img );
  2. $ Content = unpack ("H *", $ content); // Obtain the hexadecimal representation of image data

2.

'\ XFE' indicates four characters

"\ XFE" indicates 1 character (that is, the ascii character corresponding to the hexadecimal number)

You can use chr ('0xfe ') to convert single quotes.

Printer printing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.