I coded style, first to show you the effect of the picture, the parents feel the effect is good, very satisfied, please continue to read down.
Before, also wrote the use of Android two-dimensional code to generate color two-dimensional code and logo two-dimensional code, also know that you can use QRCode and zxing two ways, and then this one is also to write two-dimensional code using Barcodeformat.qr_code, The main is also see a lot of very beautiful two-dimensional code, here is the main imitation of the two-dimensional code QQ, and also high imitation to achieve a long press to send friends and save to the library function, feel good, please more support, where not good can also say out. All right, let's take it one step at a while.
The first step: simple two-dimensional code implementation
First, the simplest two-dimensional code:
Look at the simple code implementation:
/** * Generate a custom wide-high two-dimensional code image based on the specified content * @param content * Need to generate the contents of two-dimensional code * @param width * Two-dimensional code width * @param height * Two-dimensional code height * @throws Writer Exception * Generates a two-dimensional code exception/public static Bitmap Makeqrimage (String content, int width, int height) throws Writerexception {Ha
Shtable<encodehinttype, string> hints = new Hashtable<encodehinttype, string> ();
Hints.put (Encodehinttype.character_set, "UTF-8");
Image data conversion, using matrix conversion Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (content, barcodeformat.qr_code, width, height, hints);
int[] pixels = new int[width * height]; According to the two-dimensional code algorithm, a two-dimensional code of the image generation, two for loop is the result of the picture row scan for (int y = 0; y < height; y++) {for (int x = 0; x < width x + +) {if bi
Tmatrix.get (x, y))//range for Black pixels[y * width + x] = 0xff000000;
else//Other place for white pixels[y * width + x] = 0xFFFFFFFF;
}//Generate two-dimensional code picture format, using argb_8888 Bitmap Bitmap = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
Set the range of pixel matrices bitmap.setpixels (pixels, 0, width, 0, 0, width, height);
return bitmap; }
Step two: Simple two-dimensional code plus logo
Next to the two-dimensional code plus logo: (see picture)
/** * Generates a custom wide-high two-dimensional code image based on the specified content * * param LOGOBM * logo icon * param content * Need to generate the contents of two-dimensional code * param width * Two-dimensional code width * param Height * Two Dimension Code Height * throws Writerexception * Generates two-dimensional code exception/public static Bitmap Makeqrimage (Bitmap logobmp, String content, int qr_width, int qr_height) throws Writerexception {try {//image data conversion, using matrix conversion hashtable<encodehinttype, object> hints = new Hashta
Ble<encodehinttype, object> ();
Hints.put (Encodehinttype.character_set, "utf-8"); Hints.put (Encodehinttype.error_correction, ErrorCorrectionLevel.H);//fault-tolerant rate hints.put (Encodehinttype.margin, 2);
Default is 4 hints.put (encodehinttype.max_size, 350);
Hints.put (encodehinttype.min_size, 100);
Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (content, Barcodeformat.qr_code, qr_width, qr_height, hints);
int[] pixels = new int[qr_width * Qr_height]; for (int y = 0; y < qr_height; y++) {//Here follows the two-dimensional code algorithm, which generates a two-dimensional code picture,//Two for loop is the result of the picture row scan for (int x = 0; x < qr_width x + +) {if (Bitmatrix.get (x, y)) pixels[y * qr_width + x] = 0xff000000;
else Pixels[y * qr_width + x] = 0xFFFFFFFF;
}//------------------Add Picture part------------------//Bitmap Bitmap = Bitmap.createbitmap (Qr_width, Qr_height,
Bitmap.Config.ARGB_8888);
Set pixel points bitmap.setpixels (pixels, 0, qr_width, 0, 0, qr_width, qr_height);
Get picture wide high int logowidth = Logobmp.getwidth ();
int logoheight = Logobmp.getheight (); if (qr_width = 0 | | Qr_height = = 0) {return null;} if (logowidth = 0 | | logoheight = 0) {return bitmap;}//Picture in the middle of two-dimensional code, synthetic two-dimensional code picture//Logo size
For two-dimensional code the whole size of the 1/2 float scalefactor = qr_width * 1.0F/2/logowidth; try {Canvas Canvas = new Canvas (bitmap); Canvas.drawbitmap (bitmap, 0, 0, null); Canvas.scale (Scalefactor, Scalefactor, QR
_WIDTH/2, QR_HEIGHT/2);
Canvas.drawbitmap (Logobmp, (qr_width-logowidth)/2, (Qr_height-logoheight)/2, NULL);
Canvas.save (Canvas.all_save_flag);
Canvas.restore ();
return bitmap; catch (Exception e) {bitmap = null; E.getstacktrace ();}}
catch (Writerexception e) {e.printstacktrace ();} return null; }
The previous code can be seen to the two-dimensional code picture in the middle of the logo, but the picture can not occupy a large part of the entire two-dimensional code picture. Then you must also set the fault tolerance rate: there are several levels of fault tolerance m,l,q,h, the higher the fault tolerance rate, the more effective pixel points of two-dimensional code. Here use lowercase utf-8 encoding, uppercase will appear]q2\000026 beginning content, in order to look at the point also set the margin and size.
Step three: Realize the color two-dimensional code with logo
Next we turn the Black-and-white matrix into a color matrix:
Just put
if (Bitmatrix.get (x, y))
pixels[y * width + x] = 0xff000000;
else
pixels[y * width + x] = 0xFFFFFFFF;
To be replaced by: (The color here casually set, the effect casually changed)
if (x < qr_width/2 && y < QR_HEIGHT/2) {
Pixels[y * qr_width + x] = 0xff0094ff;//Blue
Integer.toh Exstring (New Random (). Nextint ());
else if (x < qr_width/2 && y > Qr_height/2) {
Pixels[y * qr_width + x] = 0xfffed545;//Yellow
} else if (x > Qr_width/2 && y > Qr_height/2) {
Pixels[y * qr_width + x] = 0xff5acf00;//Green
} else {
pixels[y * qr_width + x] = 0xff000000;//black
}
} else {
pixels[y * qr_width + x] = 0xffffffff;//White
}
The effect after the change:
Fourth step: Add the background to the two-dimensional code
Next we'll add a background to the two-dimensional code picture:
/**
* To the two-dimensional code picture plus background
*
*
/public static Bitmap Addbackground (Bitmap foreground,bitmap background) {
int bgwidth = Background.getwidth ();
int bgheight = Background.getheight ();
int fgwidth = Foreground.getwidth ();
int fgheight = Foreground.getheight ();
Bitmap Newmap = Bitmap
. CreateBitmap (Bgwidth, Bgheight, Bitmap.Config.ARGB_8888);
Canvas Canvas = new Canvas (newmap);
Canvas.drawbitmap (background, 0, 0, null);
Canvas.drawbitmap (foreground, (bgwidth-fgwidth)/2,
(bgheight-fgheight) *3/5+70, null);
Canvas.save (Canvas.all_save_flag);
Canvas.restore ();
return newmap;
}
This effect becomes:
Fifth step: Add watermark to two dimensional code
Then the two-dimensional code of personalized production on the last step: add watermark, Position casually put
/**
* In the lower right corner of the picture add Watermark
* *
@param srcbmp * Original *
@param markbmp
* watermark Picture
* @return The image after the synthetic watermark
*/< C9/>public static Bitmap Composewatermark (Bitmap srcbmp, Bitmap markbmp) {
if (srcbmp = null) {return
Null;
}
//Create a new bitmap with the same width as src length
Bitmap newb = Bitmap.createbitmap (Srcbmp.getwidth (),
srcbmp.getheight () , Bitmap.Config.ARGB_8888);
Canvas CV = new Canvas (NEWB);
At 0, 0 coordinates begin to draw into the original artwork
Cv.drawbitmap (srcbmp, 0, 0, null);
In the lower-right corner of the original artwork, draw the watermark
Cv.drawbitmap (markbmp, Srcbmp.getwidth ()-Markbmp.getwidth () *4/5,
srcbmp.getheight () *2/7 , null);
Save
Cv.save (Canvas.all_save_flag);
Storage
Cv.restore ();
return newb;
}
Here is the complete code class that implements the two-dimensional code personalization:
Package com.ry.personalizedcode.uitls;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.EncodeHintType;
Import com.google.zxing.WriterException;
Import Com.google.zxing.common.BitMatrix;
Import Com.google.zxing.qrcode.QRCodeWriter;
Import Com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
Import java.util.Hashtable;
Import Java.util.Random;
/** * Created on 2016/2/24. * Generate two-dimensional code of the tool class/public class Makeqrcodeutil {/** * Generate custom wide-high two-dimensional code images based on specified content * * param LOGOBM * logo icon * param content * need to generate two-dimensional code Content * param width * Two-dimensional code width * param Height * Two-dimensional code height * throws writerexception * Generate two-dimensional code exception/public static Bitmap Makeqrimage (Bi TMap logobmp, String content, int qr_width, int qr_height) throws Writerexception {try {//image data conversion, using matrix conversion hashtable<e
Ncodehinttype, object> hints = new Hashtable<encodehinttype, object> (); Hints.put (Encodehinttype.charActer_set, "Utf-8"); Hints.put (Encodehinttype.error_correction, ErrorCorrectionLevel.H);//fault-tolerant rate hints.put (Encodehinttype.margin, 2);
Default is 4 hints.put (encodehinttype.max_size, 350);
Hints.put (encodehinttype.min_size, 100);
Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (content, Barcodeformat.qr_code, qr_width, qr_height, hints);
int[] pixels = new int[qr_width * Qr_height]; for (int y = 0; y < qr_height; y++) {//Here follows the two-dimensional code algorithm, which generates a two-dimensional code picture,//Two for loop is the result of the picture row scan for (int x = 0; x < qr_width x + +) {if (Bitmatrix.get (x, y)) {if (x < qr_width/2 && y < QR_HEIGHT/2) {Pixels[y * qr_width + x] = 0xF
f0094ff;//Blue integer.tohexstring (New Random (). Nextint ()); else if (x < qr_width/2 && y > Qr_height/2) {pixels[y * qr_width + x] = 0xfffed545;//Yellow} else if (x > Qr_width/2 && y > Qr_height/2) {pixels[y * qr_width + x] = 0xff5acf00;//Green} else {Pixels[y * qr_w Idth + x] = 0xff000000;//Black}} else {Pixels[y * qr_width + x] = 0xffffffff;//White}}//------------------Add Picture part------------------//Bitmap Bitmap = Bitmap.createbitmap (Qr_width, qr_he
ight, Bitmap.Config.ARGB_8888);
Set pixel points bitmap.setpixels (pixels, 0, qr_width, 0, 0, qr_width, qr_height);
Get picture wide high int logowidth = Logobmp.getwidth ();
int logoheight = Logobmp.getheight (); if (qr_width = 0 | | Qr_height = = 0) {return null;} if (logowidth = 0 | | logoheight = 0) {return bitmap;}//Picture in the middle of two-dimensional code, synthetic two-dimensional code picture//Logo size
For two-dimensional code the whole size of the 1/2 float scalefactor = qr_width * 1.0F/2/logowidth; try {Canvas Canvas = new Canvas (bitmap); Canvas.drawbitmap (bitmap, 0, 0, null); Canvas.scale (Scalefactor, Scalefactor, QR
_WIDTH/2, QR_HEIGHT/2);
Canvas.drawbitmap (Logobmp, (qr_width-logowidth)/2, (Qr_height-logoheight)/2, NULL);
Canvas.save (Canvas.all_save_flag);
Canvas.restore ();
return bitmap; catch (Exception e) {bitmap = null; E.getstacktrace ();}}
catch (Writerexception e) {e.printstacktrace ();} return null; /** * Gets the hexadecimal color code. For example, "#6E36B4", for HTML, * @return String */public static string Getrandcolorcode () {string r,g,b;
Random Random = new Random ();
R = integer.tohexstring (Random.nextint (256)). toUpperCase ();
g = integer.tohexstring (Random.nextint (256)). toUpperCase ();
b = integer.tohexstring (Random.nextint (256)). toUpperCase (); R = r.length () ==1?
"0" + r:r; g = G.length () ==1?
"0" + g:g; b = b.length () ==1?
"0" + b:b;
return r+g+b; /** * Generates a custom wide-high two-dimensional code image based on the specified content * @param content * Need to generate the contents of two-dimensional code * @param width * Two-dimensional code width * @param height * Two-dimensional code height * @throws Write Rexception * Generates a two-dimensional code exception/public static Bitmap Makeqrimage (String content, int width, int height) throws Writerexception {H
Ashtable<encodehinttype, string> hints = new Hashtable<encodehinttype, string> ();
Hints.put (Encodehinttype.character_set, "UTF-8");
Image data conversion, using matrix conversion Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (content, barcodeformat.qr_code, width, height, hints);
int[] pixels = new int[width * height]; According to the two-dimensional code algorithm, a two-dimensional code of the image generation, two for loop is the result of the picture row scan for (inT y = 0; Y < height; y++) {for (int x = 0; x < width; x + +) {if (Bitmatrix.get (x, y)) pixels[y * width + x] = 0xff000000; else Pixels[y * W
Idth + x] = 0xFFFFFFFF;
}//Generate two-dimensional code picture format, using argb_8888 Bitmap Bitmap = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
Bitmap.setpixels (pixels, 0, width, 0, 0, width, height);
return bitmap; /** * Get Picture from resource file * * @param context * @param Drawableid * resource file ID * @return/public static Bitmap Gainbitmap (Cont
ext context, int drawableid) {Bitmap bmp = Bitmapfactory.decoderesource (Context.getresources (), Drawableid); /** * In the lower right corner of the picture add watermark * * @param srcbmp * Original * @param markbmp * Watermark Picture * @return The image after the watermark/public static Bitmap Composewater Mark (Bitmap srcbmp, Bitmap markbmp) {if (srcbmp = null) {return null;}//Create a new and src length-width bitmap Bitmap newb = Bitmap.cre
Atebitmap (Srcbmp.getwidth (), Srcbmp.getheight (), Bitmap.Config.ARGB_8888);
Canvas CV = new Canvas (NEWB);
At 0, 0 coordinates begin to draw into the original artwork Cv.drawbitmap (srcbmp, 0, 0, NULL); In the original artwork ofThe lower right corner is drawn into the watermark Cv.drawbitmap (markbmp, Srcbmp.getwidth ()-Markbmp.getwidth () *4/5, Srcbmp.getheight () *2/7, NULL);
Save Cv.save (Canvas.all_save_flag);
Storage Cv.restore ();
return newb; /** * to the two-dimensional code picture plus background * */public static Bitmap Addbackground (Bitmap foreground,bitmap background) {int bgwidth = BACKGROUND.G
Etwidth ();
int bgheight = Background.getheight ();
int fgwidth = Foreground.getwidth ();
int fgheight = Foreground.getheight ();
Bitmap Newmap = Bitmap. CreateBitmap (Bgwidth, Bgheight, Bitmap.Config.ARGB_8888);
Canvas Canvas = new Canvas (NEWMAP);
Canvas.drawbitmap (background, 0, 0, NULL);
Canvas.drawbitmap (foreground, (bgwidth-fgwidth)/2, (bgheight-fgheight) *3/5+70, NULL);
Canvas.save (Canvas.all_save_flag);
Canvas.restore ();
return newmap; }
}
Sixth step: To achieve the two-dimensional code long by function
Finally in order to simulate the QQ view two-dimensional code card function, but also added a long press pop-up Actionsheet function.
See Effect:
Specific Android version of the implementation of Actionsheet, the previous blog has introduced the need to please.
Here we first implement send to friend function: (here is not to do a third party send)
private void Sendtofriends () {
Intent intent=new Intent (intent.action_send);
Uri imageuri= Uri.parse (environment.getexternalstoragedirectory () + "/code/qrcode.jpg");
Intent.settype ("image/*");
Intent.putextra (Intent.extra_stream, Imageuri);
Intent.setflags (intent.flag_activity_new_task);
StartActivity (Intent.createchooser (Intent, GetTitle ()));
Send to friends Effect chart:
Then it is to implement the functionality saved to this map library:
/**
* First save to local and broadcast to gallery
*/public
static void Saveimagetogallery (context, Bitmap BMP) {
//First save picture C5/>file appdir = new File (Environment.getexternalstoragedirectory (),
"code");
if (!appdir.exists ()) {
appdir.mkdir ();
}
String fileName = "qrcode.jpg";
File = new file (Appdir, fileName);
try {
FileOutputStream fos = new FileOutputStream (file);
Bmp.compress (compressformat.jpeg, FOS);
Fos.flush ();
Fos.close ();
} catch (FileNotFoundException e) {
e.printstacktrace ();
} catch (IOException e) {
e.printstacktrace (); c21/>}
//Next insert the file into the System gallery
try {
MediaStore.Images.Media.insertImage (context.getcontentresolver), File.getabsolutepath (), fileName, null);
Finally, notify the gallery update
context.sendbroadcast (Intent.action_media_scanner_scan_file, Uri.parse ("file://")
+ file));
} catch (FileNotFoundException e) {
e.printstacktrace ();
}
}
Summarize:
This is a blunt use of a large number of canvas and bitmap processing, and then the length is a bit long, it seems a bit tired. To see the complete code, please download Personalizedcode.rar yourself. The next article I am going to write the two-dimensional code picture in WebView to identify the two-dimensional code function.