本文整理汇总了Java中com.google.zxing.qrcode.QRCodeWriter类的典型用法代码示例。如果您正苦于以下问题:Java QRCodeWriter类的具体用法?Java QRCodeWriter怎么用?Java QRCodeWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
QRCodeWriter类属于com.google.zxing.qrcode包,在下文中一共展示了QRCodeWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getQR
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
public static Bitmap getQR(final byte[] content, final int size) {
final QRCodeWriter writer = new QRCodeWriter();
try {
final Map<EncodeHintType, Object> encodingHints = new HashMap<>();
encodingHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
encodingHints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
final BitMatrix encoding = writer.encode(Utils.makeQR(content), BarcodeFormat.QR_CODE, size, size, encodingHints);
final Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
bitmap.setPixel(i, j, encoding.get(i, j) ? COLOR_DARK : COLOR_LIGHT);
}
}
return bitmap;
} catch (WriterException e) {
Log.e("QRUtils", "Failed to get QR code", e);
}
return null;
}
示例2: createQRCode
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
/**
* Create QR code
*
* @param content content
* @param widthPix widthPix
* @param heightPix heightPix
* @param openErrorCorrectionLevel Does fault tolerance open?
* @param logoBitmap The two-dimensional code Logo icon (Center for null)
* @param filePath The file path used to store two-dimensional code images
* @return Generate two-dimensional code and save the file is successful [boolean]
*/
public static boolean createQRCode(String content, int widthPix, int heightPix, boolean openErrorCorrectionLevel, Bitmap logoBitmap, String filePath) {
try {
if (TextUtils.isEmpty(content) || TextUtils.equals("null", content) || "".equals(content)) {
return false;
}
Map hints = openErrorCorrectionLevel(openErrorCorrectionLevel);
// Image data conversion, the use of matrix conversion
BitMatrix bitMatrix = new QRCodeWriter().encode(new String(content.getBytes("UTF-8"), "iso-8859-1"), BarcodeFormat.QR_CODE, widthPix, heightPix, hints);
Bitmap bitmap = generateQRBitmap(bitMatrix);
if (logoBitmap != null) {
bitmap = addLogo(bitmap, logoBitmap);
}
boolean compress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));
//You must use the compress method to save the bitmap to the file and then read it.
//The bitmap returned directly is without any compression, and the memory consumption is huge!
return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));
} catch (WriterException | IOException e) {
e.printStackTrace();
}
return false;
}
示例3: create
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
public static @NonNull Bitmap create(String data) {
try {
BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512);
Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
for (int y = 0; y < result.getHeight(); y++) {
for (int x = 0; x < result.getWidth(); x++) {
if (result.get(x, y)) {
bitmap.setPixel(x, y, Color.BLACK);
}
}
}
return bitmap;
} catch (WriterException e) {
Log.w(TAG, e);
return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
}
}
示例4: createQrCode
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
@Nullable
static Bitmap createQrCode(DisplayMetrics dm, String input) {
int smallestDimen = Math.min(dm.widthPixels, dm.heightPixels);
try {
// Generate QR code
final BitMatrix encoded = new QRCodeWriter().encode(
input, QR_CODE, smallestDimen, smallestDimen);
// Convert QR code to Bitmap
int width = encoded.getWidth();
int height = encoded.getHeight();
int[] pixels = new int[width * height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixels[y * width + x] = encoded.get(x, y) ? BLACK : WHITE;
}
}
Bitmap qr = Bitmap.createBitmap(width, height, ARGB_8888);
qr.setPixels(pixels, 0, width, 0, 0, width, height);
return qr;
} catch (WriterException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return null;
}
}
示例5: generateQr
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
public static void generateQr(final OnQrGeneratedListener onQrGeneratedListener, final String seed) {
new Thread(new Runnable() {
@Override
public void run() {
final QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix bitMatrix = writer.encode(seed, BarcodeFormat.QR_CODE, 512, 512);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
onQrGeneratedListener.onQrGenerated(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
}).start();
}
示例6: printQRCodeInTerminal
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
/**
* 关于Ansi Color的信息,参考:http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
*
* @param text
*/
public static void printQRCodeInTerminal(String text) {
int qrcodeWidth = 400;
int qrcodeHeight = 400;
HashMap<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints);
// 将像素点转成格子
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
for (int y = 30; y < height - 30; y += 10) {
for (int x = 30; x < width - 20; x += 10) {
boolean isBlack = bitMatrix.get(x, y);
System.out.print(isBlack ? " " : "\u001b[47m \u001b[0m");
}
System.out.println();
}
}
catch (WriterException e) {
e.printStackTrace();
}
}
示例7: generate
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
public Bitmap generate(String text, int width, int height) {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
try {
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (bitMatrix.get(j, i)) {
pixels[i * width + j] = 0x00000000;
} else {
pixels[i * height + j] = 0xffffffff;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
bitmap = addLogo(bitmap);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
示例8: generateQRCode
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
public Bitmap generateQRCode(String text) {
Bitmap bmp = null;
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage
int size = 256;
BitMatrix bitMatrix = null;
try {
bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hintMap);
int width = bitMatrix.getWidth();
bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < width; y++) {
bmp.setPixel(y, x, bitMatrix.get(x, y) == true ? Color.BLACK : Color.WHITE);
}
}
} catch (WriterException e) {
e.printStackTrace();
}
return bmp;
}
示例9: generateWifiQrCode
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
/**
* Generate a QR code containing the given Wi-Fi configuration
*
* @param width the width of the QR code
* @param wifiNetwork the Wi-Fi configuration
* @return a bitmap representing the QR code
* @throws WriterException if the Wi-Fi configuration cannot be represented in the QR code
*/
public static Bitmap generateWifiQrCode(int width, WifiNetwork wifiNetwork) throws WriterException {
int height = width;
com.google.zxing.Writer writer = new QRCodeWriter();
String wifiString = getWifiString(wifiNetwork);
BitMatrix bitMatrix = writer.encode(wifiString, BarcodeFormat.QR_CODE, width, height);
Bitmap imageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
imageBitmap.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK : Color.WHITE);
}
}
return imageBitmap;
}
示例10: create2dBarcodeBitmap
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
public static Bitmap create2dBarcodeBitmap(String input, int size) {
try {
final QRCodeWriter barcodeWriter = new QRCodeWriter();
final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
final BitMatrix result = barcodeWriter.encode(input, 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) ? Color.BLACK : Color.WHITE;
}
}
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (final Exception e) {
e.printStackTrace();
return null;
}
}
示例11: createQrCodeBitmap
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
protected Bitmap createQrCodeBitmap(String input, int size) {
Log.d(Config.LOGTAG,"qr code requested size: "+size);
try {
final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
final BitMatrix result = QR_CODE_WRITER.encode(input, 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) ? Color.BLACK : Color.TRANSPARENT;
}
}
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Log.d(Config.LOGTAG,"output size: "+width+"x"+height);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (final WriterException e) {
return null;
}
}
示例12: encode
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
/**
* Encode a QR code
* @param string, the string to encode
* @param encodingOptions, encoding options
* @return A new QRPlusInfo
* @throws WriterException
*/
public static QRPlusInfo encode(String string, Map<EncodeHintType, Object> encodingOptions) throws WriterException
{
QRCodeWriter writer = new QRCodeWriter();
BitMatrix q = writer.encode(
string,
BarcodeFormat.QR_CODE, 0, 0, encodingOptions );
return new QRPlusInfo(string, q);
}
示例13: createQRCode
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
/**
* 生成二维码
*
* @param text 需要生成二维码的文字、网址等
* @param size 需要生成二维码的大小()
* @return bitmap
*/
public static Bitmap createQRCode(String text, int size) {
try {
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new QRCodeWriter().encode(text,
BarcodeFormat.QR_CODE, size, size, hints);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
pixels[y * size + x] = 0xffffffff;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
示例14: genQRCode
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
/**
* 生成二维码
*
* @param url 二维码包含的URL信息
* @return Bitmap
*/
@SuppressWarnings("SuspiciousNameCombination")
private Bitmap genQRCode(String url) {
// 等待placeholder绘制完成之后调用
int width = mQRCodeImage.getMeasuredWidth();
Bitmap bitmap = null;
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix bm = writer.encode(url, BarcodeFormat.QR_CODE, width, width);
bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
bitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
}
}
} catch (WriterException e) {
Log.w(TAG, e.toString());
}
return bitmap;
}
示例15: generateQrCodeBitmap
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
private static Bitmap generateQrCodeBitmap(@NonNull final String value) throws WriterException {
final QRCodeWriter writer = new QRCodeWriter();
final int size = BaseApplication.get().getResources().getDimensionPixelSize(R.dimen.qr_code_size);
final Map<EncodeHintType, Integer> map = new HashMap<>();
map.put(EncodeHintType.MARGIN, 0);
final BitMatrix bitMatrix = writer.encode(value, BarcodeFormat.QR_CODE, size, size, map);
final int width = bitMatrix.getWidth();
final int height = bitMatrix.getHeight();
final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
final int contrastColour = ContextCompat.getColor(BaseApplication.get(), R.color.windowBackground);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : contrastColour);
}
}
return bmp;
}