QR code-java generates a QR code business card and automatically saves it to the mobile phone address book...
Technology qq exchange group: JavaDream: 251572072
1. First introduce an api.
Zxing is a bar code provided by Google.
The parsing tool provides the generation and resolution of QR codes. Now I will briefly introduce how to use Java to generate and parse QR codes using Zxing.
2. QR code generation
1.1 Add the Zxing-core.jar package to classpath.
1.2 The MatrixToImageWriter class is used to generate a QR code,
This class is provided by Google and can be copied to the source code. Here I paste the source code of this class and can be directly used.
Import com. google. zxing. common. BitMatrix;
Import javax. imageio. ImageIO;
Import java. io. File;
Import java. io. OutputStream;
Import java. io. IOException;
Import java. awt. image. BufferedImage;
Public final class MatrixToImageWriter {
Private static final int BLACK = 0xFF000000;
Private static final int WHITE = 0 xFFFFFFFF;
Private MatrixToImageWriter (){}
Public static BufferedImage toBufferedImage (BitMatrix matrix ){
Int width = matrix. getWidth ();
Int height = matrix. getHeight ();
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
For (int x = 0; x <width; x ++ ){
For (int y = 0; y Image. setRGB (x, y, matrix. get (x, y )? BLACK: WHITE );
}
}
Return image;
}
Public static void writeToFile (BitMatrix matrix, String format, File file)
Throws IOException {
BufferedImage image = toBufferedImage (matrix );
If (! ImageIO. write (image, format, file )){
Throw new IOException ("cocould not write an image of format" + format + "to" + file );
}
}
Public static void writeToStream (BitMatrix matrix, String format, OutputStream stream)
Throws IOException {
BufferedImage image = toBufferedImage (matrix );
If (! ImageIO. write (image, format, stream )){
Throw new IOException ("cocould not write an image of format" + format );
}
}
}
------------------------------------------------------------------------
2. Create a javaproject to generate a business card.
------------------------------------------
The project structure is as follows:
ErWerMa
/ErWerMa/src
Credream
Credream. main
/ErWerMa/src/credream/main/CreQcode. java
Credream. utils
/ErWerMa/src/credream/utils/MatrixToImageWriter. java
/ErWerMa/src/Zxing-core.jar
-------------------------------------------------------------
Package credream. main;
Import java. io. File;
Import java. util. HashMap;
Import java. util. Map;
Import com. google. zxing. BarcodeFormat;
Import com. google. zxing. EncodeHintType;
Import com. google. zxing. MultiFormatWriter;
Import com. google. zxing. common. BitMatrix;
Import credream. utils. MatrixToImageWriter;
Public class CreQcode {
Public static void main (String [] args ){
Try {
// 1. The following is a format used for my test.
// BEGIN: VCARD
// "VERSION: 3.0
// N: Li dewei
// EMAIL: 1606841559@qq.com
// TEL: 12345678912
// TEL; CELL: 12345678912
// ADR: Shandong Jinan Qilu software park
// ORG: Jinan
// TITLE: Software Engineer
// URL: http://blog.csdn.net/lidew521
// NOTE: Call for testing...
// END: VCARD"
// 2. Reasoning Based on test results
//---------------------------------------
// The test results won't work if you do not press Enter. In this case, problems may occur.
// NO content will be found after scanning
// Here we can see the method of parsing the QR code
//-------------------------------------------------
// 3. The test \ n can be identified by the QR code
// There is also a reason, because after scanning the QR code, it will be processed twice,
// Java code is used during processing. Because it is an android system
// In java, the carriage return is \ n, SO \ n is used to separate them.
String content = "BEGIN: VCARD \ n" +
& Quot; VERSION: 3.0 \ n & quot; +
"N: Li dewei \ n" +
"EMAIL: 1606841559@qq.com \ n" +
"TEL: 12345678912" +
"TEL; CELL: 12345678912" +
"ADR: Shandong Jinan Qilu software park \ n" +
"ORG:" +
"Jinan \ n" +
"TITLE: Software Engineer \ n" +
"URL: http://blog.csdn.net/lidew521824n" +
"NOTE: let's test it... \ N "+
"END: VCARD ";
String path = "D:/4 S ";
MultiFormatWriter multiFormatWriter = new MultiFormatWriter ();
Map hints = new HashMap ();
Hints. put (EncodeHintType. CHARACTER_SET, "UTF-8 ");
BitMatrix bitMatrix = multiFormatWriter. encode (content, BarcodeFormat. QR_CODE, 400,400, hints );
File file1 = new File (path, "credits .jpg ");
MatrixToImageWriter. writeToFile (bitMatrix, "jpg", file1 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
-----------------------------------------------------------------------
3. You can do it now.
After scanning, the SAVE page is automatically displayed, and you can choose to save it to the address book.
------------------------------------------------------------------
4. Analysis:
A. Some people may think that the scan is a page and the QR code contains a page similar to jsp.
In fact, this is wrong.
B. It is proved that the scanned content can be added to the address book by using qq or qq, but it is not possible to use ordinary QR code scanning software.
This indicates that the two-dimensional code only stores a formatted string of data.
C. The reason why the scan can be in the contact format is because internal processing is done. The processing process here should be like this.
Call the QR code api to parse the QR code.
D. Fill in the parsed content to implement the prepared jsp page. Then, there is a section in this jsp page.
You can contact the java method and js Code in the program, so that you can call the address book of the android system through the java method in.
(This part may not be clear. In android, js and java methods can call each other to transfer values. You can learn about android, address book, and reference .)
Android tutorials I have previously written)