Android generates a QR code with an image,

Source: Internet
Author: User
Tags vcard

Android generates a QR code with an image,

I. Problem Description

In development, we need to convert the information into two-dimensional code storage and require the company logo. We know that Google's Zxing open-source project can help us to generate and parse the barcode and two-dimensional code, however, the official website with a logo does not provide a demo. The following describes how to implement and use Zxing through examples.

Ii. Case studies
1. Case Running Effect

 

2. case preparation

To add jar to a project, you only need to add core. jar

Zxing project: https://github.com/zxing/zxing/

Iii. Main Zxing Components
1. BarcodeFormat

Different binary encoding methods are defined. The values are as follows:

EAN_13 bar code, a total of 13 codes, more common, such as the packaging on the product is such a bar code

CODE_QR code (Matrix Code) is more popular than bar code.

The CODE_128 barcode can contain 127 characters from ASCII 0 to ASCII 128. It is used for enterprise management and production process control.

CODE_39 barcode. The compilation simply accepts the following 43 characters:

2. MultiFormatWriter

It mainly includes an encode () method to generate code (bar and QR code)

BitMatrix encode (String contents, BarcodeFormat format, int width, int height, Hashtable hints) Method

  Parameters:

Contents: content to be encoded

Format: encoding format (Bar, two-dimensional)

Width, height: the size of the generated code

Hints: a collection of EncodeHintType information. It mainly sets character encoding. For example, UTF-8 of Chinese characters is supported, as shown below:

Hashtable <EncodeHintType, String> hst = new Hashtable <EncodeHintType, String> ();

Hst. put (EncodeHintType. CHARACTER_SET, "UTF-8 ");

  Return Value: BitMatrix two-dimensional matrix point

3. BitMatrix

BitMatrix: a two-dimensional matrix. x indicates the position of the column, and y indicates the position of the row. The column is arranged in sequence from the upper left corner (x to y)

  Main Method:

GetWidth (): returns the width of the matrix.

GetHeight (): returns the height of the matrix.

Boolean get (x, y): a very important method to determine whether a Black Block exists at the position based on the given x and y.

In the application that generates a QR code, this method is used to determine, record the points with Black Blocks, generate a graph using the setPixels () method of Bitmap, and illustrate the createCode () method of the case () code in the Method

Iv. complete code
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, affected intermediate image size @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the resource image. You can change it to get a local image or take a photo to get the image 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 the QR code String contents = "BEGIN: VCARD \ nVERSION: 3.0 \ nORG:" + company + "\ nTEL: "+ phone +" \ nURL: "+ web +" \ nEMAIL: "+ email +" \ nEND: VCARD "; try {// call the createCode method to generate the QR code Bitmap bm = createCode (contents, logo, BarcodeFormat. QR_CODE); ImageView img = (ImageView) findViewById (R. id. imgCode); // display the img on the QR code page. setImageBitmap (bm);} catch (WriterException e) {// TODO Auto-generated catch block e. printStackTrace ();}}});} /*** generate a QR code ** @ param string the text information contained in the QR code * @ param mBitmap logo image * @ param format encoding format * @ return 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 // scale the logo image to the information set by martix 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"); // sets the character encoding BitMatrix matrix = writer. encode (string, format, 400,400, hst); // generate a two-dimensional code matrix. int width = matrix. getWidth (); // matrix height int height = matrix. getHeight (); // matrix width int halfW = width/2; int halfH = height/2; int [] pixels = new int [width * height]; // define the length of the array as the height of the matrix * the width of the matrix, used to record the pixel information in the matrix for (int y = 0; y In addition, if you need to save the generated QR code to a disk, refer to the savebmptosd () method of bitmaputilin blog http://www.cnblogs.com/jerehedu/p/4461090.html

  

To learn more, clickView Source CodeIn person!

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.

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.