- private static final class EncodeThread extends Thread {
- private static final String TAG = "EncodeThread";
-
- private final String contents;
- private final Handler handler;
- private final int pixelResolution;
- private final BarcodeFormat format;
-
- EncodeThread(String contents, Handler handler, int pixelResolution,
- BarcodeFormat format) {
- this.contents = contents;
- this.handler = handler;
- this.pixelResolution = pixelResolution;
- this.format = format;
+ static Bitmap encodeAsBitmap(String contents,
+ BarcodeFormat format,
+ int desiredWidth,
+ int desiredHeight) throws WriterException {
+ Hashtable hints = null;
+ String encoding = guessAppropriateEncoding(contents);
+ if (encoding != null) {
+ hints = new Hashtable(2);
+ hints.put(EncodeHintType.CHARACTER_SET, encoding);
+ }
+ MultiFormatWriter writer = new MultiFormatWriter();
+ BitMatrix result = writer.encode(contents, format, desiredWidth, desiredHeight, hints);
+ int width = result.getWidth();
+ int height = result.getHeight();
+ int[] pixels = new int[width * height];
+ // All are 0, or black, by default
+ for (int y = 0; y < height; y++) {
+ int offset = y * width;
+ for (int x = 0; x < width; x++) {
+ pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
+ }