It was launched on 360 for a month and has downloaded more than a thousand times. Share all the code here for everyone to learn! It also includes teaching you how to connect to advertisements, make some money and spend money, and help you to help someone you like. Don’t criticize when you see it. Elementary school monks have just learned Android not long ago. First, let me introduce this application: APP is a QR code generator. Although there are many tutorials on how to make QR codes on the Internet, I will nag here and share all my functional module codes.
Here we need an auxiliary class RGBLuminanceSource, which is also provided by Google. We can use it by pasting it directly.
package ; import ; import ; import ; import ; public final class RGBLuminanceSource extends LuminanceSource { private final byte[] luminances; public RGBLuminanceSource(String path) throws FileNotFoundException { this(loadBitmap(path)); } public RGBLuminanceSource(Bitmap bitmap) { super((), ()); int width = (); int height = (); int[] pixels = new int[width * height]; (pixels, 0, width, 0, 0, width, height); // In order to measure pure decoding speed, we convert the entire image // to a greyscale array // up front, which is the same as the Y channel of the // YUVLuminanceSource in the real app. luminances = new byte[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { int pixel = pixels[offset + x]; int r = (pixel >> 16) & 0xff; int g = (pixel >> 8) & 0xff; int b = pixel & 0xff; if (r == g && g == b) { // Image is already greyscale, so pick any channel. luminances[offset + x] = (byte) r; } else { // Calculate luminance cheaply, favoring green. luminances[offset + x] = (byte) ((r + g + g + b) >> 2); } } } } @Override public byte[] getRow(int y, byte[] row) { if (y < 0 || y >= getHeight()) { throw new IllegalArgumentException( "Requested row is outside the image:"+ y); } int width = getWidth(); if (row == null || < width) { row = new byte[width]; } (luminances, y * width, row, 0, width); return row; } // Since this class does not support cropping, the underlying byte array // already contains // exactly what the caller is asking for, so give it to them without a copy. @Override public byte[] getMatrix() { return luminances; } private static Bitmap loadBitmap(String path) throws FileNotFoundException { Bitmap bitmap = (path); if (bitmap == null) { throw new FileNotFoundException("Couldn't open"+ path); } return bitmap; } }
public Bitmap getTwoDimensionPicture(String text,int width,int height) throws WriterException{ if(("")) { text=""; } Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); (EncodeHintType.CHARACTER_SET,"utf-8"); BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); int []pixels = new int[width*height]; for(int y=0;y<height;y++){ for(int x=0;x<width;x++){ if ((x, y)) { pixels[y * width + x] = BLACK; } else { pixels[y * width + x] = WHITE; } } } Bitmap bitmap=(width, height,.ARGB_8888); (pixels, 0,width, 0, 0, width, height); return bitmap; }
public void createDirctoryToSaveImage(){ String dirPath=()++"TowDimensionCode"; File dirFile=new File(dirPath); if(!()){ (); } } public void writeBitMapToSDCard(Bitmap bitmap) throws IOException{ String fname = ("yyyyMMddhhmmss", new Date()).toString()+".jpg"; String filePath=()++"TowDimensionCode" ++fname; File file=new File(filePath); FileOutputStream fileOutputStream=new FileOutputStream(file); (, 100, fileOutputStream); (); (); //Add the picture into the system gallery(getApplicationContext().getContentResolver(), (), fname, null); //uri gets the absolute path to the file getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, ("file://"+()))); (()); (this,"Generate Successfully", Toast.LENGTH_LONG).show(); }
//Open the album private void setImage() { //Use intent to call the album function provided by the system, use startActivityForResult to obtain the pictures selected by the user. Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT); (IMAGE_TYPE); startActivityForResult(getAlbum, IMAGE_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ if (resultCode != RESULT_OK) { //The RESULT_OK here is a constant that is customized by the system ("TAG->onresult","ActivityResult resultCode error"); return; } Bitmap bm = null; //Outsiders can access the data provided by ContentProvider through the ContentResolver interface ContentResolver resolver = getContentResolver(); //The activity you are here used to determine whether the received activity is the one you want if (requestCode == IMAGE_CODE) { try { Uri originalUri = (); //Get the uri of the picture bm = (resolver, originalUri); //Import the bitmap image(bm); //The second part that starts here is to get the path to the picture: String[] proj = {}; //It seems to be the encapsulation interface of the Android multimedia database. For details, please refer to the Android documentation. Cursor cursor = managedQuery(originalUri, proj, null, null, null); //According to my personal understanding, this is to obtain the index value of the image selected by the user. int column_index = (); //Move the cursor to the beginning, this is very important, and it can easily cause cross-border if you accidentally(); //Finally get the image path based on the index value String path = (column_index); (path); (); }catch (IOException e) { ("TAG-->Error",()); } } }
/** * Analyze the contents in the QR code picture * @param filePath QR code picture location * @throws IOException * @throws NotFoundException */ private String readImage(ImageView imageView) { String content = null; Map<DecodeHintType, String> hints = new HashMap<DecodeHintType, String>(); (DecodeHintType.CHARACTER_SET,"utf-8"); // Get the picture to be parsed Bitmap bitmap = ((BitmapDrawable) ()).getBitmap(); RGBLuminanceSource source = new RGBLuminanceSource(bitmap); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); try { Result result = (bitmap1, hints); // Get the parsed text content = (); } catch (Exception e) { (); } return content; }
//ad layout part private RelativeLayout adContainer = null; private IMvBannerAd bannerad = null; final String adSpaceid ="This is the advertising ID number you applied for"; adContainer=(RelativeLayout)findViewById(); bannerad = (adContainer, this, adSpaceid, false); (this);
The monthly download volume is thousands of times. Don’t miss the source code of Android QR code generator app!