Android generates two-dimensional code with images

Source: Internet
Author: User
<span id="Label3"></p> <table border="0" 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"> <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‘">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 border="0" 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"> <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‘">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 border="0" 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"> <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‘">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 border="0" 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"> <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 ' "> four, full code </span> </strong></td> </tr> </tbody> </table><pre><pre name="code" class="java">public class Mainactivity extends Activity {private EditText etcompany; Private EditText etphone; Private EditText etemail; Private EditText etweb; Private Bitmap logo; private static final int image_halfwidth = 40;//width value, affecting the middle picture size @Override protected void onCreate (Bundle savedinstances Tate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); To get a picture of a resource, you can change it to get a local picture or take a picture Logo=bitmapfactory.decoderesource (super.getresources (), r.drawable.y014); Etcompany = (EditText) Findviewbyid (r.id.etcompany); etphone= (EditText) Findviewbyid (r.id.etphone); Etemail = (EditText) Findviewbyid (r.id.etemail); Etweb = (EditText) Findviewbyid (r.id.etweb); Findviewbyid (r.id.but). setonclicklistener (new onclicklistener () {@Override public void OnClick (View V) {//TODO auto-generated Method stub String company=etcompany.gettext (). toString (). Trim ( ) ; String phone =etphone. getText (). toString (). trim (); String email = etemail.gettext (). toString (). trim (); String web = etweb.gettext (). toString (). trim (); Text information contained in a QR code String contents= "begin:vcard\nversion:3.0\norg:" +company+ "\ntel:" +phone+ "\nurl:" +web+ "\nemai L: "+email+" \nend:vcard "; Try {//call method Createcode generate two-dimensional code Bitmap Bm=createcode (contents,logo,barcodeformat.qr_code); ImageView img= (ImageView) Findviewbyid (r.id.imgcode); The two-dimensional code is displayed in the interface Img.setimagebitmap (bm); } catch (writerexception E) {//TODO auto-generated catch block E.printstacktrace (); } } }); /** * Generate QR Code * @param text information contained in a string QR code * @param mbitmap logo image * @param format encoding * @return B Itmap bitmap * @throws writerexception */public Bitmap Createcode (String string,bitmap mbitmap, barcodeformat format) throws writerexception {Matrix m = new Matrix (); float SX = (float) 2 * image_halfwidth/mbitmap.getwidth (); float sy = (float) 2 * image_halfwidth/mbitmap.getheight (); M.setscale (sx, Sy);//set The scaling Information//martix the logo image by the information set mbitmap = Bitmap.createbitmap (mbitmap, 0, 0, Mbitmap.getwidth (), mbitmap.getheight (), m, false); Multiformatwriter writer = new Multiformatwriter (); hashtable<encodehinttype, string> HST = new hashtable<encodehinttype, string> (); Hst.put (encodehinttype.character_set, "UTF-8");//set character encoding Bitmatrix matrix = Writer.encode (string, format, 400, 400, Hst);//generate two-dimensional code matrix information int width = matrix.getwidth ();//matrix height int height = matrix.getheight ();//matrix width int half W = width/2; int HALFH = height/2; int[] pixels = new Int[width * Height];//defines the array length as the matrix height * Matrix width, which is used to record pixels in the matrixFor (int y = 0; y < height; y++) {//start with the line iteration matrix for (int x = 0; x < width; + +) {//iterate columns If (x > Halfw-image_halfwidth && x < HALFW + image_halfwidth && y > H Alfh-image_halfwidth && y < HALFH + Image_halfwidth) {//this location is used to store picture Information//record pictures per pixel information Pixels[y * width + x] = mbitmap.getpixel (X-HALFW + image_halfwidth, Y-HALFH + image_halfwidth); } else {if (matrix.get (x, y)) {//if There is a black dot, record information pixels[y * width + x] = 0xff000000 ;//record Black block information}}} Bitmap Bitmap = Bitmap.createbitmap (width, He ight, Bitmap.Config.ARGB_8888); Generates bitmap bitmap.setpixels (pixels, 0, width, 0, 0, width, height) through the array of pixels; Return bitmap; } }</pre></pre><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"></span></span></span>http://blog.csdn.net/jerehedu<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 CSDN common, welcome reprint, but without the author's consent 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.