package org.odk.collect.android.utilities; /* * Copyright (C) 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.EnumMap; import java.util.Map; import android.graphics.Bitmap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; /** * * @author ZXing authors * @author csims@dimagi.com * * Pulled from the ZXing libraries and modified slightly (mostly just pulled out code we don't need) * */ public final class QRCodeEncoder { private static final int WHITE = 0xFFFFFFFF; private static final int BLACK = 0xFF000000; private int dimension = Integer.MIN_VALUE; private String contents = null; private BarcodeFormat format = BarcodeFormat.QR_CODE; public QRCodeEncoder(String data, int dimension) { this.dimension = dimension; contents = data; } public Bitmap encodeAsBitmap() throws WriterException { Map<EncodeHintType, Object> hints = null; String encoding = guessAppropriateEncoding(contents); if (encoding != null) { hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = writer.encode(contents, format, dimension, dimension, 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; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } private static String guessAppropriateEncoding(CharSequence contents) { // Very crude at the moment for (int i = 0; i < contents.length(); i++) { if (contents.charAt(i) > 0xFF) { return "UTF-8"; } } return null; } }