package com.mygeopay.wallet.util; /* * Copyright 2013-2014 the original author or authors. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Hashtable; import java.util.Map; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import javax.annotation.Nonnull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import android.graphics.Bitmap; import android.graphics.Color; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.encoder.Encoder; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.encoder.QRCode; /** * @author Andreas Schildbach */ public class Qr { private final static QRCodeWriter QR_CODE_WRITER = new QRCodeWriter(); private static final Logger log = LoggerFactory.getLogger(Qr.class); private static final ErrorCorrectionLevel ERROR_CORRECTION_LV = ErrorCorrectionLevel.H; private static final int darkColor = 0xdd000000; private static final int lightColor = 0; public static Bitmap bitmap(@Nonnull final String content, final int maxSize) { try { QRCode code = Encoder.encode(content, ERROR_CORRECTION_LV, null); int baseSize = code.getMatrix().getWidth(); // Make qr-code size multiples of baseSize int size = (int) (Math.max(Math.floor(maxSize / baseSize), 1) * baseSize); final Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.MARGIN, 0); hints.put(EncodeHintType.ERROR_CORRECTION, ERROR_CORRECTION_LV); final BitMatrix result = QR_CODE_WRITER.encode(content, BarcodeFormat.QR_CODE, size, size, hints); final int width = result.getWidth(); final int height = result.getHeight(); final int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { final int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? darkColor : lightColor; } } final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (final WriterException x) { log.info("problem creating qr code", x); return null; } } public static String encodeCompressBinary(@Nonnull final byte[] bytes) { try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length); final GZIPOutputStream gos = new GZIPOutputStream(bos); gos.write(bytes); gos.close(); final byte[] gzippedBytes = bos.toByteArray(); final boolean useCompressioon = gzippedBytes.length < bytes.length; final StringBuilder str = new StringBuilder(); str.append(useCompressioon ? 'Z' : '-'); str.append(Base43.encode(useCompressioon ? gzippedBytes : bytes)); return str.toString(); } catch (final IOException x) { throw new RuntimeException(x); } } public static String encodeBinary(@Nonnull final byte[] bytes) { return Base43.encode(bytes); } public static byte[] decodeDecompressBinary(@Nonnull final String content) throws IOException { final boolean useCompression = content.charAt(0) == 'Z'; final byte[] bytes = Base43.decode(content.substring(1)); InputStream is = new ByteArrayInputStream(bytes); if (useCompression) is = new GZIPInputStream(is); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte[] buf = new byte[4096]; int read; while (-1 != (read = is.read(buf))) baos.write(buf, 0, read); baos.close(); is.close(); return baos.toByteArray(); } public static byte[] decodeBinary(@Nonnull final String content) throws IOException { return Base43.decode(content); } }