當前位置: 首頁>>代碼示例>>Java>>正文


Java QRCodeWriter類代碼示例

本文整理匯總了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;
}
 
開發者ID:mDL-ILP,項目名稱:mDL-ILP,代碼行數:23,代碼來源:QRUtils.java

示例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;
}
 
開發者ID:Jusenr,項目名稱:androidtools,代碼行數:36,代碼來源:QRCodeUtils.java

示例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);
  }
}
 
開發者ID:XecureIT,項目名稱:PeSanKita-android,代碼行數:20,代碼來源:QrCode.java

示例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;
	}
}
 
開發者ID:rafjordao,項目名稱:Nird2,代碼行數:25,代碼來源:QrCodeUtils.java

示例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();
}
 
開發者ID:GrenderG,項目名稱:Protestr,代碼行數:23,代碼來源:ImageUtils.java

示例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();
    }
}
 
開發者ID:Lovelcp,項目名稱:Easy-WeChat,代碼行數:30,代碼來源:QRCodeUtil.java

示例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;
}
 
開發者ID:m2049r,項目名稱:xmrwallet,代碼行數:25,代碼來源:ReceiveFragment.java

示例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;
}
 
開發者ID:victordiaz,項目名稱:phonk,代碼行數:26,代碼來源:PMedia.java

示例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;
}
 
開發者ID:bparmentier,項目名稱:WiFiKeyShare,代碼行數:25,代碼來源:QrCodeUtils.java

示例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;
    }
}
 
開發者ID:syntafin,項目名稱:TenguChat,代碼行數:24,代碼來源:BarcodeProvider.java

示例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;
	}
}
 
開發者ID:xavierle,項目名稱:messengerxmpp,代碼行數:25,代碼來源:XmppActivity.java

示例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);
}
 
開發者ID:RandomReaper,項目名稱:qr2gerber,代碼行數:16,代碼來源:QRPlusInfo.java

示例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;
    }
}
 
開發者ID:backkomyoung,項目名稱:Metro,代碼行數:33,代碼來源:QRCodeUtil.java

示例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;
}
 
開發者ID:YieldNull,項目名稱:Biu,代碼行數:28,代碼來源:ConnectingActivity.java

示例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;
}
 
開發者ID:toshiapp,項目名稱:toshi-android-client,代碼行數:18,代碼來源:ImageUtil.java


注:本文中的com.google.zxing.qrcode.QRCodeWriter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。