If you think the Android project bitfynd-wallet-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2013-2014 the original author or authors.
*//fromwww.java2s.com
* 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/>.
*/package de.schildbach.wallet.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
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.decoder.ErrorCorrectionLevel;
/**
* @author Andreas Schildbach
*/publicclass Qr
{
privatefinalstatic QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
privatestaticfinal Logger log = LoggerFactory.getLogger(Qr.class);
publicstatic Bitmap bitmap(@Nonnull final String content, finalint size)
{
try
{
final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.MARGIN, 0);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
final BitMatrix result = QR_CODE_WRITER.encode(content, BarcodeFormat.QR_CODE, size, size, hints);
finalint width = result.getWidth();
finalint height = result.getHeight();
finalint[] pixels = newint[width * height];
for (int y = 0; y < height; y++)
{
finalint offset = y * width;
for (int x = 0; x < width; x++)
{
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
}
}
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;
}
}
publicstatic String encodeCompressBinary(@Nonnull finalbyte[] bytes)
{
try
{
final ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
final GZIPOutputStream gos = new GZIPOutputStream(bos);
gos.write(bytes);
gos.close();
finalbyte[] gzippedBytes = bos.toByteArray();
finalboolean 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)
{
thrownew RuntimeException(x);
}
}
publicstatic String encodeBinary(@Nonnull finalbyte[] bytes)
{
return Base43.encode(bytes);
}
publicstaticbyte[] decodeDecompressBinary(@Nonnull final String content) throws IOException
{
finalboolean useCompression = content.charAt(0) == 'Z';
finalbyte[] bytes = Base43.decode(content.substring(1));
InputStream is = new ByteArrayInputStream(bytes);
if (useCompression)
is = new GZIPInputStream(is);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
finalbyte[] buf = newbyte[4096];
int read;
while (-1 != (read = is.read(buf)))
baos.write(buf, 0, read);
baos.close();
is.close();
return baos.toByteArray();
}
publicstaticbyte[] decodeBinary(@Nonnull final String content) throws IOException
{
return Base43.decode(content);
}
}