Teach you to create a QR code-google.zxing

Source: Internet
Author: User

First, the goal

Enter the URL to generate the QR code of the URL

Ii. Overview

1. Effect: The UI is ugly, but the function is implemented

2. Project directory

Iii. third-party resources used

1.google Sweep Code Pack zxing

2.JQuery

Iv. steps (with MyEclipse)

1. Create a new project, select Web Project

2. Join third-party resources (zxing package and jquery)

2.1

2.1.1 Copy, paste, add zxing jar package (all files are downloaded in the attachment)

2.2.2 Right-click Jar Package Join BuildPath

2.2 Add jquery File

2.2.1 Create a new folder under Webroot JS

Copy and paste the jquery file to this folder

3. Modify the index.jsp page under Webroot as required:

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

4. Create a new servlet file

4.1 Right-click Project Folder--"new---->servlet (MyEclipse automatically configures servlet to Web. xml file)

The code for the Qrcode.java servlet file is as follows:

Package Servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import util. Qrcodeutil;import Com.google.zxing.writerexception;public class QRCode extends HttpServlet {public void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException { Request.setcharacterencoding ("Utf-8"); Response.setcharacterencoding ("Utf-8"); String website = request.getparameter ("website"); try {qrcodeutil.gengr (website, response.getoutputstream ());} catch ( Writerexception e) {e.printstacktrace ();} String website = request.getattribute ("website"). ToString (); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}

5. Create a new auxiliary class file to generate a QR code (QRCODEUTIL.JAVA)

5.1 Right-click Project Folder-"New--->class, such as

The code for Qrcodeutil.java is as follows:

Package Util2;import Java.awt.image.bufferedimage;import Java.io.ioexception;import java.io.outputstream;import Java.util.hashtable;import Javax.imageio.imageio;import Com.google.zxing.barcodeformat;import Com.google.zxing.encodehinttype;import Com.google.zxing.multiformatwriter;import Com.google.zxing.writerexception;import Com.google.zxing.common.bitmatrix;public class QRCodeUtil {private static Final int BLACK = 0xff000000;private static final int white = 0xffffffff;public static void Gengr (String website, outputst Ream output) throws Writerexception, IOException {int width = 300;int height = 300;        String format = "JPG";         Hashtable<encodehinttype, string> hints = new Hashtable<encodehinttype, string> ();        Hashtable hints = new Hashtable (); Hints.put (Encodehinttype.character_set, "utf-8"); Bitmatrix BM = new Multiformatwriter (). Encode (website, barcodeformat.qr_code, width, height, hints); BufferedImage image = Toimage (BM); Imageio.write (image, format, OUTPUt);//writes the QR code to the output stream of response}private static bufferedimage Toimage (Bitmatrix BM) {int width = bm.getwidth (); int height = Bm.get Height (); BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb), for (INT x = 0;x < width; x + +) {for (in T y = 0; Y < height; y++) {Image.setrgb (x, Y, Bm.get (x, y)? black:white);}} return image;}}

6. Now that all the files have been completed, the next setup project, launch Tomcat, access the files, such as

When the layout is complete, click Debug to start the project

After successful startup, access the file in the browser

Ps:

You can modify the color configuration in the Qrcodeutil.java file to generate a color QR code, for example:

private static final int BLACK = 0xff0000ff;//0xff000000;
private static final int white = 0XFFFFFF00;//0XFFFFFFFF;

Common color codes are as follows:

These color constants are defined in the Android.graphics.Color:

Type

Constant

Value

color code

Int

BLACK

-16777216

0xff000000

Int

BLUE

-16776961

0xff0000ff

Int

CYAN

-16711681

0xff00ffff

Int

Dkgray

-12303292

0xff444444

Int

GRAY

-7829368

0xff888888

Int

GREEN

-16711936

0xff00ff00

Int

Ltgray

-3355444

0xffcccccc

Int

MAGENTA

-65281

0xffff00ff

Int

RED

-65536

0xffff0000

Int

TRANSPARENT

0

0x00000000

Int

White

-1

0xFFFFFFFF

Int

YELLOW

-256

0xffffff00

Project Annex:

Http://files.cnblogs.com/files/shamgod/QRCode.zip

Teach you to create a QR code-google.zxing

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.