Android generates two-dimensional code with images

Source: Internet
Author: User
<span id="Label3"></p> <table style="border: 0px solid #000000; border-collapse: collapse; word-break: break-word; color: #333333; font-family: Georgia, ‘Times New Roman‘, Times, sans-serif; font-size: 14px; line-height: 25px; width: 100%; height: 38px; background-color: #1570a6;" border="0"> <tbody> <tr> <td style="border-style: solid; border-color: silver; border-collapse: collapse; padding: 3px;"><strong><span style="color: #ffffff; font-size: 14pt; font-family: ‘Microsoft YaHei‘; text-shadow: #222222 2px 2px 3px;">first, The problem description</span></strong></td> </tr> </tbody> </table><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">In the development of the need to convert information into two-dimensional code storage and requirements with the Company's logo, we know that Google's zxing Open source project is very good to help us realize the barcode, QR code generation and resolution, but with the logo of the official website does not provide demo, Here's An example of how to implement and use Zxing.</span></p></p> <table style="border: 0px solid #000000; border-collapse: collapse; word-break: break-word; color: #333333; font-family: Georgia, ‘Times New Roman‘, Times, sans-serif; font-size: 14px; line-height: 25px; width: 100%; height: 38px; background-color: #1570a6;" border="0"> <tbody> <tr> <td style="border-style: solid; border-color: silver; border-collapse: collapse; padding: 3px;"><strong><span style="color: #ffffff; font-size: 14pt; font-family: ‘Microsoft YaHei‘; text-shadow: #222222 2px 2px 3px;">second, Case Introduction</span></strong></td> </tr> </tbody> </table>1. Case Operation effect<p><p></p></p>2, Case Preparation Work<p><p><span style="font-family: ‘Microsoft YaHei‘;">To add a jar to your project, simply add Core.jar</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Zxing Project Address: https://github.com/zxing/zxing/</span></p></p> <table style="border: 0px solid #000000; border-collapse: collapse; word-break: break-word; color: #333333; font-family: Georgia, ‘Times New Roman‘, Times, sans-serif; font-size: 14px; line-height: 25px; width: 100%; height: 38px; background-color: #1570a6;" border="0"> <tbody> <tr> <td style="border-style: solid; border-color: silver; border-collapse: collapse; padding: 3px;"><strong><span style="color: #ffffff; font-size: 14pt; font-family: ‘Microsoft YaHei‘; text-shadow: #222222 2px 2px 3px;">three, zxing Main components</span></strong></td> </tr> </tbody> </table>1, Barcodeformat<p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Defines the different binary encoding methods, with the following values</span></p></p> <blockquote> <blockquote> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Ean_13 barcode, A total of 13-digit code, more common, such as the packaging on the commodity is the barcode</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">CODE_QR QR Code (matrix code), More information than the bar code, the present more popular</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">code_128 Barcode can represent a total of 128 characters from ASCII 0 to ASCII 127 for enterprise management, production Process Control</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">code_39 barcode, Easy to prepare only accepts the following 43 characters</span></p> </blockquote> </blockquote>2, Multiformatwriter<p><p><span style="font-family: ‘Microsoft YaHei‘;">Mainly includes a encode () method, which can generate code (bar, QR Code)</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Bitmatrix encode (String contents, barcodeformat format, int width, int height,</span><span style="font-family: ‘Microsoft YaHei‘;">Hashtable Hints) method</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">  <span style="color: #3366ff;"><strong>Parameters</strong> <span style="color: #000000;">:</span></span></span></p></p> <blockquote> <blockquote> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Contents: What to encode</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Format: encoded format (bar, two-dimensional)</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Width,height: the size of the generated code</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Hints: contains a collection of Encodehinttype (encoding hint Type) information, mainly set character encoding, such as supporting the utf-8 of Chinese characters, as Follows:</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">hashtable<encodehinttype, string> HST = new hashtable<encodehinttype, string> ();</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Hst.put (encodehinttype.character_set, "UTF-8");</span></p> </blockquote> </blockquote><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">  <strong><span style="color: #3366ff;">return value</span></strong> : Bitmatrix two-dimensional matrix point</span></p></p>3, Bitmatrix<p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Bitmatrix: represented as a two-dimensional matrix, x for the position of the column, y for the position of the row, starting from the Upper-left corner, and a column-by-column arrangement (first x after Y)</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">  <strong><span style="color: #3366ff;">Main methods</span></strong> :</span></p></p> <blockquote> <blockquote> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">GetWidth (): Returns the width of the matrix</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">GetHeight (): Returns the height of the matrix</span></p> <p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Boolean get (x, y): a very important method to determine if there is a black block in the position given x, y</span></p> </blockquote> </blockquote><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">In the application that produces the QR code, it is judged by this method, then the point of the black block is recorded, and the setpixels () method of bitmap is used to generate the graph, and the code in the Createcode () method of the case is Detailed.</span></p></p> <table style="border: 0px solid #000000; border-collapse: collapse; word-break: break-word; color: #333333; font-family: Georgia, ‘Times New Roman‘, Times, sans-serif; font-size: 14px; line-height: 25px; width: 100%; height: 38px; background-color: #1570a6;" border="0"> <tbody> <tr> <td style="border-style: solid; border-color: silver; border-collapse: collapse; padding: 3px;"><strong><span style="color: #ffffff; font-size: 14pt; font-family: ‘Microsoft YaHei‘; text-shadow: #222222 2px 2px 3px;">Iv. Complete code</span></strong></td> </tr> </tbody> </table><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">EditText etcompany; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">EditText etphone; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">EditText etemail; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">EditText etweb; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Bitmap logo; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Image_halfwidth = 40;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">width value, which affects the middle picture size</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">get <span style="color: #008000;">a picture of a resource and change it to get a local picture or take a photo to get a picture</span></span>Logo=bitmapfactory.decoderesource (<span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Getresources (), r.drawable.y014); Etcompany</span></span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.etcompany); Etphone</span></span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.etphone); Etemail</span></span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.etemail); Etweb</span></span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.etweb); Findviewbyid (r.id.but). Setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>String company=<span style="color: #000000;"><span style="color: #000000;">etcompany.gettext (). toString (). trim (); String Phone</span></span>=<span style="color: #000000;"><span style="color: #000000;">etphone. getText (). toString (). trim (); String Email</span></span>=<span style="color: #000000;"><span style="color: #000000;">etemail.gettext (). toString (). trim (); String Web</span></span>=<span style="color: #000000;"><span style="color: #000000;">etweb.gettext (). toString (). trim (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">text information contained in a QR code</span></span>String contents= "begin:vcard\nversion:3.0\norg:" +company+ "\ntel:" +phone+ "\nurl:" +web+ "\nemail:" +email+ "\nEND: VCARD "<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Call method Createcode generate two-dimensional code</span></span>Bitmap bm=<span style="color: #000000;"><span style="color: #000000;">Createcode (contents,logo,barcodeformat.qr_code); ImageView img</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ImageView) Findviewbyid (r.id.imgcode); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Display the QR code in the interface</span></span><span style="color: #000000;"><span style="color: #000000;">Img.setimagebitmap (bm); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(writerexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } } }); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Generate QR Code *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">text information contained in a string QR code *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Mbitmap logo image *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Format Encoding *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;">Bitmap Bitmap *</span></span><span style="color: #808080;"><span style="color: #808080;">@throws</span></span><span style="color: #008000;"><span style="color: #008000;">writerexception</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Bitmap createcode (String string,bitmap mbitmap, barcodeformat format)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">writerexception {Matrix m</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Matrix (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>SX = (<span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>) 2 * image_halfwidth/<span style="color: #000000;"><span style="color: #000000;">Mbitmap.getwidth (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>SY = (<span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>) 2 *<span style="color: #000000;"><span style="color: #000000;">Image_halfwidth</span></span>/<span style="color: #000000;"><span style="color: #000000;">Mbitmap.getheight (); M.setscale (sx, sy);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set Scaling information</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Zoom the logo image to the information set by Martix</span></span>Mbitmap = Bitmap.createbitmap (mbitmap, 0, 0<span style="color: #000000;"><span style="color: #000000;">, Mbitmap.getwidth (), mbitmap.getheight (), m,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">); Multiformatwriter writer</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Multiformatwriter (); Hashtable</span></span><encodehinttype, string> HST =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>hashtable<encodehinttype, string><span style="color: #000000;"><span style="color: #000000;">(); Hst.put (encodehinttype.character_set,</span></span>"UTF-8");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set character encoding</span></span>Bitmatrix matrix = Writer.encode (string, format, n, i, hst);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Generate two-dimensional code matrix information</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>width = matrix.getwidth ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Matrix Height</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Height = matrix.getheight ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Matrix Width</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>HALFW = WIDTH/2<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>HALFH = HEIGHT/2<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>[] pixels =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>[width * height];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">defines the array length as the matrix height * Matrix width for recording pixel information in the matrix</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>y = 0; Y < height; Y++) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">start Iteration matrix from line</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>x = 0; X < width; X + +) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Iterate Columns</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(x > Halfw-image_halfwidth && x < HALFW +<span style="color: #000000;"><span style="color: #000000;">Image_halfwidth</span></span>&& y > halfh-<span style="color: #000000;"><span style="color: #000000;">Image_halfwidth</span></span>&& y < halfh + Image_halfwidth) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This location is used to store picture information</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">record pictures per pixel information</span></span>Pixels[y * width + x] = Mbitmap.getpixel (x-<span style="color: #000000;"><span style="color: #000000;">HALFW</span></span>+ image_halfwidth, Y-HALFH + image_halfwidth); }<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(matrix.get (x, Y)) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If there is a black dot, record the information</span></span>Pixels[y * width + x] = 0xff000000;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Log Black block information</span></span><span style="color: #000000;">}}}} <span style="color: #000000;">Bitmap Bitmap</span></span>=<span style="color: #000000;"><span style="color: #000000;">bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">generating bitmap through the array</span> of pixels</span>Bitmap.setpixels (pixels, 0, width, 0, 0<span style="color: #000000;"><span style="color: #000000;">, width, height); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">bitmap; } }</span></span></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">View Code</span></span><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">alternatively, If you need to save the resulting QR code to disk, refer to the Bitmaputil savebmptosd () method in the blog http://www.cnblogs.com/jerehedu/p/4464870.html</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">  </span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">To learn more about the students, you can click to <strong><span style="font-size: 16px;">view the source code</span></strong> , personally run the experience!</span></p></p><p><p></p></p>Jerry Education<br>Source:<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">http://www.cnblogs.com/jerehedu/</span></span></span><br><span style="font-family: ‘Microsoft YaHei‘; color: #000000;"><span style="font-family: ‘Microsoft YaHei‘; color: #000000;">This article is the copyright of <span style="color: #000000;">Yantai Jerry Education Technology co., Ltd.</span> and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. </span></span><p><p>Android generates two-dimensional code with images</p></p></span>

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.