-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeGenerator.java
114 lines (92 loc) · 3.31 KB
/
CodeGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package tools.amolood.qrpanda.utility;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import java.util.Arrays;
/**
* Created by Amolood on 7/21/2016.
*/
public class CodeGenerator extends AsyncTask<Void, Void, Bitmap>{
public static final int QR_DIMENSION = 1080, BAR_HEIGHT = 640, BAR_WIDTH = 1080;
private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;
private ResultListener resultListener;
private String input;
private static int TYPE, TYPE_QR = 0, TYPE_BAR = 1;
public void generateQRFor(String input) {
this.input = input;
TYPE = TYPE_QR;
}
public void generateBarFor(String input) {
this.input = input;
TYPE = TYPE_BAR;
}
public void setResultListener(ResultListener resultListener) {
this.resultListener = resultListener;
}
@Override
protected Bitmap doInBackground(Void... voids) {
try {
if (TYPE == TYPE_QR) {
return createQRCode(this.input);
} else {
return createBarcode(this.input);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if(resultListener != null) {
resultListener.onResult(bitmap);
}
}
public interface ResultListener {
public void onResult(Bitmap bitmap);
}
private Bitmap createQRCode(String str) throws WriterException {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, QR_DIMENSION, QR_DIMENSION, null);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int w = result.getWidth();
int h = result.getHeight();
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
int offset = y * w;
for (int x = 0; x < w; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, QR_DIMENSION, 0, 0, w, h);
return bitmap;
}
private Bitmap createBarcode(String data) throws WriterException {
MultiFormatWriter writer = new MultiFormatWriter();
String finalData = Uri.encode(data);
// Use 1 as the height of the matrix as this is a 1D Barcode.
BitMatrix bm = writer.encode(finalData, BarcodeFormat.CODE_128, BAR_WIDTH, 1);
int bmWidth = bm.getWidth();
Bitmap imageBitmap = Bitmap.createBitmap(bmWidth, BAR_HEIGHT, Bitmap.Config.ARGB_8888);
for (int i = 0; i < bmWidth; i++) {
// Paint columns of width 1
int[] column = new int[BAR_HEIGHT];
Arrays.fill(column, bm.get(i, 0) ? Color.BLACK : Color.WHITE);
imageBitmap.setPixels(column, 0, 1, i, 0, 1, BAR_HEIGHT);
}
return imageBitmap;
}
}