Android download thousands of times a month to achieve the TWO DIMENSIONAL code generator app source sharing
- 2020-11-25 07:31:29
- OfStack
It has been online on 360 for one month and has been downloaded thousands of times. Here to share the code, for everyone to learn ha! Also includes teaching everyone how to access advertising, make a little money to spend, like to help the top 1, the great god saw don't spray, the little monk just learned Android not long ago. First of all, APP is a 2-d code generator. Although there are many tutorials on how to make 2-D code on the Internet, I will talk about it again and share all my functional module code.
In this case, we need a helper class RGBLuminanceSource, which is also provided by Google, so we can just paste it in and use it
package com.njupt.liyao;
import com.google.zxing.LuminanceSource;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.FileNotFoundException;
public final class RGBLuminanceSource extends LuminanceSource {
private final byte[] luminances;
public RGBLuminanceSource(String path) throws FileNotFoundException {
this(loadBitmap(path));
}
public RGBLuminanceSource(Bitmap bitmap) {
super(bitmap.getWidth(), bitmap.getHeight());
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(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 || row.length < width) {
row = new byte[width];
}
System.arraycopy(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 = BitmapFactory.decodeFile(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.equals(""))
{
text="";
}
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(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 (bitMatrix.get(x, y))
{
pixels[y * width + x] = BLACK;
}
else
{
pixels[y * width + x] = WHITE;
}
}
}
Bitmap bitmap=Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0,width, 0, 0, width, height);
return bitmap;
}
public void createDirctoryToSaveImage(){
String dirPath=Environment.getExternalStorageDirectory()+File.separator+"TowDimensionCode";
File dirFile=new File(dirPath);
if(!dirFile.exists()){
dirFile.mkdir();
}
}
public void writeBitMapToSDCard(Bitmap bitmap) throws IOException{
String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString()+".jpg";
String filePath=Environment.getExternalStorageDirectory()+File.separator+"TowDimensionCode"
+File.separator+fname;
File file=new File(filePath);
FileOutputStream fileOutputStream=new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
// Add images to the system gallery
MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
file.getAbsolutePath(), fname, null);
//uri You get the absolute path of the file
getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"+file.getAbsolutePath())));
edtText.setText(file.getAbsolutePath());
Toast.makeText(this," Generate success ", Toast.LENGTH_LONG).show();
}
// Open the photo album
private void setImage() {
// use intent Call the album function provided by the system and use it startActivityForResult Is to get the image that the user selected
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
getAlbum.setType(IMAGE_TYPE);
startActivityForResult(getAlbum, IMAGE_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode != RESULT_OK) { // Here the RESULT_OK Is the system custom 1 A constant
Log.e("TAG->onresult","ActivityResult resultCode error");
return;
}
Bitmap bm = null;
// External program access ContentProvider Provided data Can be achieved by ContentResolver interface
ContentResolver resolver = getContentResolver();
// Used here to determine the received Activity Is it the one you want
if (requestCode == IMAGE_CODE) {
try {
Uri originalUri = data.getData(); // Picture-Acquired uri
bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);
// Look to the bitmap The picture
imgView.setImageBitmap(bm);
// This is the first one 2 Section, the path to get the picture:
String[] proj = {MediaColumns.DATA};
// Seems to be android The encapsulation interface of multimedia database, concrete see Android The document
Cursor cursor = managedQuery(originalUri, proj, null, null, null);
// As I understand it This is the index to get the image selected by the user
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
// Move the cursor to the beginning This is very important. It is easy to cross the line if you are not careful
cursor.moveToFirst();
// Finally, the image path is obtained according to the index value
String path = cursor.getString(column_index);
edtText.setText(path);
btnOpen.setText(R.string.recognitionTwoCode);
}catch (IOException e) {
Log.e("TAG-->Error",e.toString());
}
}
}
/**
* parsing 2 The contents of a dimensional image
* @param filePath 2 The location of the dimensional image
* @throws IOException
* @throws NotFoundException
*/
private String readImage(ImageView imageView) {
String content = null;
Map<DecodeHintType, String> hints = new HashMap<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET,"utf-8");
// Get the image to parse
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
Result result = reader.decode(bitmap1, hints);
// Get the text after parsing
content = result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
//ad Layout of the part
private RelativeLayout adContainer = null;
private IMvBannerAd bannerad = null;
final String adSpaceid =" Here is the AD you applied for ID No. ";
adContainer=(RelativeLayout)findViewById(R.id.adcontent);
bannerad = Mvad.showBanner(adContainer, this, adSpaceid, false);
bannerad.showAds(this);
Android download thousands of times a month to achieve 2 - dimensional code generator app source code you do not miss it!