类com.google.zxing.qrcode.decoder.ErrorCorrectionLevel源码实例Demo

下面列出了怎么用com.google.zxing.qrcode.decoder.ErrorCorrectionLevel的API类实例代码及写法,或者点击链接到github查看源代码。


static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
    throws WriterException {
  if (!QRCode.isValidMaskPattern(maskPattern)) {
    throw new WriterException("Invalid mask pattern");
  }
  int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
  bits.appendBits(typeInfo, 5);

  int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
  bits.appendBits(bchCode, 10);

  BitArray maskBits = new BitArray();
  maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
  bits.xor(maskBits);

  if (bits.getSize() != 15) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
源代码2 项目: MeetingFilm   文件: ZxingUtils.java

/** 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
    */
public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
	try {
           Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
           hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
           hints.put(EncodeHintType.CHARACTER_SET, "UTF8");

		BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);

           File imageFile = new File(imgPath);
		writeToFile(bitMatrix, "png", imageFile);

           return imageFile;

	} catch (Exception e) {
		log.error("create QR code error!", e);
           return null;
	}
}
 
源代码3 项目: Telegram-FOSS   文件: Encoder.java

private static int chooseMaskPattern(BitArray bits,
                                     ErrorCorrectionLevel ecLevel,
                                     Version version,
                                     ByteMatrix matrix) throws WriterException {

  int minPenalty = Integer.MAX_VALUE;  // Lower penalty is better.
  int bestMaskPattern = -1;
  // We try all mask patterns to choose the best one.
  for (int maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++) {
    MatrixUtil.buildMatrix(bits, ecLevel, version, maskPattern, matrix);
    int penalty = calculateMaskPenalty(matrix);
    if (penalty < minPenalty) {
      minPenalty = penalty;
      bestMaskPattern = maskPattern;
    }
  }
  return bestMaskPattern;
}
 
源代码4 项目: actframework   文件: ZXingResult.java

private void renderCode(H.Response response) {
    response.contentType("image/png");
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, Act.appConfig().encoding());
    hints.put(EncodeHintType.MARGIN, 0);
    ErrorCorrectionLevel level = errorCorrectionLevel();
    if (null != level) {
        hints.put(EncodeHintType.ERROR_CORRECTION, level);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = writer.encode(getMessage(), barcodeFormat(), width, height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, "png", response.outputStream());
    } catch (Exception e) {
        throw E.unexpected(e);
    }
}
 
源代码5 项目: RipplePower   文件: Encoder.java

private static Version chooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) throws WriterException {
	// In the following comments, we use numbers of Version 7-H.
	for (int versionNum = 1; versionNum <= 40; versionNum++) {
		Version version = Version.getVersionForNumber(versionNum);
		// numBytes = 196
		int numBytes = version.getTotalCodewords();
		// getNumECBytes = 130
		Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
		int numEcBytes = ecBlocks.getTotalECCodewords();
		// getNumDataBytes = 196 - 130 = 66
		int numDataBytes = numBytes - numEcBytes;
		int totalInputBytes = (numInputBits + 7) / 8;
		if (numDataBytes >= totalInputBytes) {
			return version;
		}
	}
	throw new WriterException("Data too big");
}
 
源代码6 项目: reacteu-app   文件: MatrixUtil.java

static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
    throws WriterException {
  if (!QRCode.isValidMaskPattern(maskPattern)) {
    throw new WriterException("Invalid mask pattern");
  }
  int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
  bits.appendBits(typeInfo, 5);

  int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
  bits.appendBits(bchCode, 10);

  BitArray maskBits = new BitArray();
  maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
  bits.xor(maskBits);

  if (bits.getSize() != 15) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
源代码7 项目: QrCodeScanner   文件: Encoder.java

private static int chooseMaskPattern(BitArray bits,
                                     ErrorCorrectionLevel ecLevel,
                                     Version version,
                                     ByteMatrix matrix) throws WriterException {

  int minPenalty = Integer.MAX_VALUE;  // Lower penalty is better.
  int bestMaskPattern = -1;
  // We try all mask patterns to choose the best one.
  for (int maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++) {
    MatrixUtil.buildMatrix(bits, ecLevel, version, maskPattern, matrix);
    int penalty = calculateMaskPenalty(matrix);
    if (penalty < minPenalty) {
      minPenalty = penalty;
      bestMaskPattern = maskPattern;
    }
  }
  return bestMaskPattern;
}
 
源代码8 项目: bither-android   文件: Qr.java

public static void printQrContentSize() {
    for (int versionNum = 1;
         versionNum <= 40;
         versionNum++) {
        Version version = Version.getVersionForNumber(versionNum);
        // numBytes = 196
        int numBytes = version.getTotalCodewords();
        // getNumECBytes = 130
        Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ErrorCorrectionLevel.L);
        int numEcBytes = ecBlocks.getTotalECCodewords();
        // getNumDataBytes = 196 - 130 = 66
        int numDataBytes = numBytes - numEcBytes;
        int numInputBytes = numDataBytes * 8 - 7;
        int length = (numInputBytes - 10) / 11 * 2;
        LogUtil.d("Qr", "Version: " + versionNum + " numData bytes: " + numDataBytes + "  " +
                "input: " + numInputBytes + "  string length: " + length);
    }
}
 
源代码9 项目: reacteu-app   文件: Encoder.java

private static Version chooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) throws WriterException {
  // In the following comments, we use numbers of Version 7-H.
  for (int versionNum = 1; versionNum <= 40; versionNum++) {
    Version version = Version.getVersionForNumber(versionNum);
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    if (numDataBytes >= totalInputBytes) {
      return version;
    }
  }
  throw new WriterException("Data too big");
}
 
源代码10 项目: ScreenCapture   文件: MatrixUtil.java

static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
    throws WriterException {
  if (!QRCode.isValidMaskPattern(maskPattern)) {
    throw new WriterException("Invalid mask pattern");
  }
  int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
  bits.appendBits(typeInfo, 5);

  int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
  bits.appendBits(bchCode, 10);

  BitArray maskBits = new BitArray();
  maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
  bits.xor(maskBits);

  if (bits.getSize() != 15) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
源代码11 项目: RipplePower   文件: MatrixUtil.java

static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits) throws WriterException {
	if (!QRCode.isValidMaskPattern(maskPattern)) {
		throw new WriterException("Invalid mask pattern");
	}
	int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
	bits.appendBits(typeInfo, 5);

	int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
	bits.appendBits(bchCode, 10);

	BitArray maskBits = new BitArray();
	maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
	bits.xor(maskBits);

	if (bits.getSize() != 15) { // Just in case.
		throw new WriterException("should not happen but we got: " + bits.getSize());
	}
}
 
源代码12 项目: seezoon-framework-all   文件: ZxingHelper.java

private BufferedImage createImage(String content, InputStream logo, boolean needCompress) throws Exception {
	Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
	hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
	hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
	hints.put(EncodeHintType.MARGIN, 1);
	BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, imageSize, imageSize,
			hints);
	int width = bitMatrix.getWidth();
	int height = bitMatrix.getHeight();
	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++) {
			image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
		}
	}
	if (logo == null) {
		return image;
	}
	// 插入logo
	this.insertLogo(image, logo, needCompress);
	return image;
}
 
源代码13 项目: letv   文件: QRCodeWriter.java

public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
    if (contents.length() == 0) {
        throw new IllegalArgumentException("Found empty contents");
    } else if (format != BarcodeFormat.QR_CODE) {
        throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
    } else if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
    } else {
        ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
        int quietZone = 4;
        if (hints != null) {
            ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
            if (requestedECLevel != null) {
                errorCorrectionLevel = requestedECLevel;
            }
            Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
            if (quietZoneInt != null) {
                quietZone = quietZoneInt.intValue();
            }
        }
        return renderResult(Encoder.encode(contents, errorCorrectionLevel, hints), width, height, quietZone);
    }
}
 
源代码14 项目: SecScanQR   文件: ScannerActivity.java

/**
 * This method creates a picture of the scanned qr code
 */
private void showQrImage() {
    multiFormatWriter = new MultiFormatWriter();
    Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);

    hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    try{
        BarcodeFormat format = generalHandler.StringToBarcodeFormat(qrcodeFormat);
        BitMatrix bitMatrix = multiFormatWriter.encode(qrcode, format, 250,250, hintMap);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
        codeImage.setImageBitmap(bitmap);
    } catch (Exception e){
        codeImage.setVisibility(View.GONE);
    }
}
 
源代码15 项目: xmrwallet   文件: ReceiveFragment.java

public Bitmap generate(String text, int width, int height) {
    if ((width <= 0) || (height <= 0)) return null;
    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 ex) {
        Timber.e(ex);
    }
    return null;
}
 
源代码16 项目: ZXing-Orient   文件: MatrixUtil.java

static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
    throws WriterException {
  if (!QRCode.isValidMaskPattern(maskPattern)) {
    throw new WriterException("Invalid mask pattern");
  }
  int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
  bits.appendBits(typeInfo, 5);

  int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
  bits.appendBits(bchCode, 10);

  BitArray maskBits = new BitArray();
  maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
  bits.xor(maskBits);

  if (bits.getSize() != 15) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
源代码17 项目: PHONK   文件: PMedia.java

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;
}
 
源代码18 项目: javautils   文件: ZxingUtil.java

/**
 * 设置二维码的参数
 * @param content
 * @param width
 * @param height
 * @return
 */
private static BitMatrix getBitMatrix(String content, int width, int height){
    /**
     * 设置二维码的参数
     */
    BitMatrix bitMatrix = null;
    try {
        Map<EncodeHintType, Object> hints = new HashMap<>(3);
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        hints.put(EncodeHintType.CHARACTER_SET, Constants.UTF8);
        //设置白边
        hints.put(EncodeHintType.MARGIN, 1);
        // 生成矩阵
        bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bitMatrix;
}
 
源代码19 项目: bicycleSharingServer   文件: QRCodeUtil.java

private static BufferedImage createImage(String content, String imgPath,
        boolean needCompress) throws Exception {
    Hashtable hints = new Hashtable();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
    hints.put(EncodeHintType.MARGIN, 1);
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
            BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    BufferedImage image = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
                    : 0xFFFFFFFF);
        }
    }
    if (imgPath == null || "".equals(imgPath)) {
        return image;
    }
    // 插入图片
    QRCodeUtil.insertImage(image, imgPath, needCompress);
    return image;
}
 
源代码20 项目: privacy-friendly-qr-scanner   文件: Utils.java

public static Bitmap generateCode(String data, BarcodeFormat format, Map<EncodeHintType, Object> hints, Map<ResultMetadataType,Object> metadata) {
    if(hints == null) {
        hints = new EnumMap<>(EncodeHintType.class);
    }

    if(!hints.containsKey(ERROR_CORRECTION) && metadata != null && metadata.containsKey(ERROR_CORRECTION_LEVEL)) {
        Object ec = metadata.get(ERROR_CORRECTION_LEVEL);
        if(ec != null) {
            hints.put(ERROR_CORRECTION, ec);
        }
    }
    if(!hints.containsKey(ERROR_CORRECTION)) {
        hints.put(ERROR_CORRECTION, ErrorCorrectionLevel.L.name());
    }

    return generateCode(data, format, hints);
}
 
源代码21 项目: Conversations   文件: BarcodeProvider.java

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);
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		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;
	}
}
 
源代码22 项目: MiBandDecompiled   文件: d.java

static void a(ErrorCorrectionLevel errorcorrectionlevel, int j, ByteMatrix bytematrix)
{
    BitArray bitarray = new BitArray();
    a(errorcorrectionlevel, j, bitarray);
    int k = 0;
    while (k < bitarray.getSize()) 
    {
        boolean flag = bitarray.get((-1 + bitarray.getSize()) - k);
        bytematrix.set(f[k][0], f[k][1], flag);
        if (k < 8)
        {
            bytematrix.set(-1 + (bytematrix.getWidth() - k), 8, flag);
        } else
        {
            bytematrix.set(8, -7 + bytematrix.getHeight() + (k - 8), flag);
        }
        k++;
    }
}
 
源代码23 项目: wish-pay   文件: ZxingUtils.java

/**
 * 将内容contents生成长为width,宽为width的图片,返回刘文静
 */
public static ServletOutputStream getQRCodeImge(String contents, int width, int height) throws IOException {
    ServletOutputStream stream = null;
    try {
        Map<EncodeHintType, Object> hints = Maps.newHashMap();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, "png", stream);
        return stream;
    } catch (Exception e) {
        log.error("create QR code error!", e);
        return null;
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
 
源代码24 项目: Telegram   文件: MatrixUtil.java

static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
    throws WriterException {
  if (!QRCode.isValidMaskPattern(maskPattern)) {
    throw new WriterException("Invalid mask pattern");
  }
  int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
  bits.appendBits(typeInfo, 5);

  int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
  bits.appendBits(bchCode, 10);

  BitArray maskBits = new BitArray();
  maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
  bits.xor(maskBits);

  if (bits.getSize() != 15) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
源代码25 项目: raccoon4   文件: QrPanel.java

/**
 * Create a new panel of a given size.
 * 
 * @param size
 *          panel width/height
 */
public QrPanel(int size) {
	this.size = size;
	qrCodeWriter = new QRCodeWriter();
	hintMap = new HashMap<EncodeHintType, Object>();
	hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
	hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
	image = BLANK;
}
 
源代码26 项目: QrCodeLib   文件: EncodingHandler.java

/**
 * 创建二维码
 *
 * @param content   content
 * @param widthPix  widthPix
 * @param heightPix heightPix
 * @param logoBm    logoBm
 * @return 二维码
 */
public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
    try {
        if (content == null || "".equals(content)) {
            return null;
        }
        // 配置参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 图像数据转换,使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                heightPix, hints);
        int[] pixels = new int[widthPix * heightPix];
        // 下面这里按照二维码的算法,逐个生成二维码的图片,
        // 两个for循环是图片横列扫描的结果
        for (int y = 0; y < heightPix; y++) {
            for (int x = 0; x < widthPix; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthPix + x] = 0xff000000;
                } else {
                    pixels[y * widthPix + x] = 0xffffffff;
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
        if (logoBm != null) {
            bitmap = addLogo(bitmap, logoBm);
        }
        //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
源代码27 项目: QrCodeScanner   文件: QRCodeWriter.java

@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {

  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (format != BarcodeFormat.QR_CODE) {
    throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
        height);
  }

  ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
  int quietZone = QUIET_ZONE_SIZE;
  if (hints != null) {
    if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
      errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
    }
    if (hints.containsKey(EncodeHintType.MARGIN)) {
      quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
    }
  }

  QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
  return renderResult(code, width, height, quietZone);
}
 
源代码28 项目: tysq-android   文件: EncodingUtils.java

/**
 * 创建二维码
 *
 * @param content   content
 * @param widthPix  widthPix
 * @param heightPix heightPix
 * @param logoBm    logoBm
 * @return 二维码
 */
public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
    try {
        if (content == null || "".equals(content)) {
            return null;
        }
        // 配置参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别 这里选择最高H级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 图像数据转换,使用了矩阵转换 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                heightPix, hints);
        int[] pixels = new int[widthPix * heightPix];
        // 下面这里按照二维码的算法,逐个生成二维码的图片,
        // 两个for循环是图片横列扫描的结果
        for (int y = 0; y < heightPix; y++) {
            for (int x = 0; x < widthPix; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthPix + x] = 0xff000000; // 黑色
                } else {
                    pixels[y * widthPix + x] = 0xffffffff;// 白色
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
        if (logoBm != null) {//绘制logo
            bitmap = addLogo(bitmap, logoBm);
        }
        //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
源代码29 项目: StarBarcode   文件: AbsCodec.java

@Override
public Bitmap encodeBarcode(String content, int widthPixel, int heightPixel) {

    if (TextUtils.isEmpty(content))
        throw new NullPointerException("The content of the QR code image cannot be empty");
    Map<EncodeHintType, Object> hintTypeMap = new EnumMap<>(EncodeHintType.class);
    hintTypeMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
    //空白边距的宽度
    hintTypeMap.put(EncodeHintType.MARGIN, 0);
    //容错级别
    hintTypeMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    try {
        //矩阵转换
        BitMatrix matrix = new MultiFormatWriter().encode(content, getEncodeFormat(), widthPixel, heightPixel, hintTypeMap);
        int[] pixels = new int[widthPixel * heightPixel];
        //使用二维码算法,逐个生成二维码的图片
        for (int y = 0; y < heightPixel; y++) {
            for (int x = 0; x < widthPixel; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * widthPixel + x] = 0xff000000; //前景色
                } else {
                    pixels[y * widthPixel + x] = 0xffffffff; //背景色
                }
            }
        }
        final Bitmap bitmap = Bitmap.createBitmap(widthPixel, heightPixel, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPixel, 0, 0, widthPixel, heightPixel);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
源代码30 项目: imsdk-android   文件: EncodingUtils.java

/**
 * 创建二维码
 *
 * @param content   content
 * @param widthPix  widthPix
 * @param heightPix heightPix
 * @param logoBm    logoBm
 * @return 二维码
 */
public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
    try {
        if (content == null || "".equals(content)) {
            return null;
        }
        // 配置参数
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 图像数据转换,使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                heightPix, hints);
        int[] pixels = new int[widthPix * heightPix];
        // 下面这里按照二维码的算法,逐个生成二维码的图片,
        // 两个for循环是图片横列扫描的结果
        for (int y = 0; y < heightPix; y++) {
            for (int x = 0; x < widthPix; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthPix + x] = 0xff000000;
                } else {
                    pixels[y * widthPix + x] = 0xffffffff;
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
        if (logoBm != null) {
            bitmap = addLogo(bitmap, logoBm);
        }
        //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
 类所在包
 同包方法