本文整理匯總了Java中com.google.zxing.qrcode.decoder.ErrorCorrectionLevel類的典型用法代碼示例。如果您正苦於以下問題:Java ErrorCorrectionLevel類的具體用法?Java ErrorCorrectionLevel怎麽用?Java ErrorCorrectionLevel使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ErrorCorrectionLevel類屬於com.google.zxing.qrcode.decoder包,在下文中一共展示了ErrorCorrectionLevel類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getQR
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
/**
* Method to get the JavaFX Image QR code for easy input of address
* @param width width of the image
* @param height height of the image
* @return Java FX Image
* @throws IOException Either when there is an encoding error or java's reserved memory is overwritten.
* @throws WriterException When ZXING encounters an error.
*/
public Image getQR(int width, int height) throws IOException, WriterException{
String charset = "UTF-8";
Map hintMap = new HashMap();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BitMatrix matrix = new MultiFormatWriter().encode(new String(cryptoAddress.getBytes(charset), charset), BarcodeFormat.QR_CODE, width, height, hintMap);
MatrixToImageWriter.writeToStream(matrix, "png", stream);
stream.flush();
byte[] data = stream.toByteArray();
stream.close();
return new Image(new ByteArrayInputStream(data));
}
示例2: getQRCodeImge
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
/**
* 將內容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();
}
}
}
示例3: getQR
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的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;
}
示例4: encode
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
/**
* Zxing圖形碼生成工具
*
* @param contents
* 內容
* @param barcodeFormat
* BarcodeFormat對象
* @param format
* 圖片格式,可選[png,jpg,bmp]
* @param width
* 寬
* @param height
* 高
* @param margin
* 邊框間距px
* @param saveImgFilePath
* 存儲圖片的完整位置,包含文件名
* @return {boolean}
*/
public static boolean encode(String contents, BarcodeFormat barcodeFormat, Integer margin,
ErrorCorrectionLevel errorLevel, String format, int width, int height, String saveImgFilePath) {
Boolean bool = false;
BufferedImage bufImg;
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 指定糾錯等級
hints.put(EncodeHintType.ERROR_CORRECTION, errorLevel);
hints.put(EncodeHintType.MARGIN, margin);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
// contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, barcodeFormat, width, height, hints);
MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
bufImg = MatrixToImageWriter.toBufferedImage(bitMatrix, config);
bool = writeToFile(bufImg, format, saveImgFilePath);
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
示例5: createImage
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
/**
* 創建二維碼
*
* @param size 二維碼尺寸
* @param content 二維碼內容
* @param logoPath Logo地址
* @param needCompress 是否壓縮
* @return
* @throws Exception
*/
private static BufferedImage createImage(int size, String content, String logoPath, 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, size, 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 (logoPath == null || "".equals(logoPath)) {
return image;
}
// 插入圖片
QRCodeUtils.insertImage(size, image, logoPath, needCompress);
return image;
}
示例6: createQRCode
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
/**
* 根據內容生成二維碼數據
* @param content 二維碼文字內容[為了信息安全性,一般都要先進行數據加密]
* @param width 二維碼照片寬度
* @param height 二維碼照片高度
* @return
*/
public static BitMatrix createQRCode(String content, int width, int height){
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
//設置字符編碼
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 指定糾錯等級
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
e.printStackTrace();
}
return matrix;
}
示例7: createQrcodeMatrix
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
/**
* 根據內容生成二維碼數據
*
* @param content 二維碼文字內容[為了信息安全性,一般都要先進行數據加密]
* @param length 二維碼圖片寬度和高度
*/
private static BitMatrix createQrcodeMatrix(String content, int length) {
Map<EncodeHintType, Object> hints = Maps.newEnumMap(EncodeHintType.class);
// 設置字符編碼
hints.put(EncodeHintType.CHARACTER_SET, Charsets.UTF_8.name());
// 指定糾錯等級
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
try {
return new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, length, length, hints);
} catch (Exception e) {
logger.warn("內容為:【" + content + "】的二維碼生成失敗!", e);
return null;
}
}
示例8: chooseMaskPattern
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
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;
}
示例9: chooseVersion
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
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: makeTypeInfoBits
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
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: encode
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
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);
}
}
示例12: generate
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的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;
}
示例13: createImage
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
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;
}
// 插入圖片
CodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
示例14: createImage
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
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;
}
示例15: createImage
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //導入依賴的package包/類
/**
*
* @param path 完整的路徑包括文件名
* @param width 圖片寬
* @param height 圖片高
* @param contents 內容
*/
public static void createImage(String path,int width,int height,String contents){
String format = "png";
//定義二維碼的參數
HashMap hints = new HashMap();
hints.put(EncodeHintType.AZTEC_LAYERS, "utf-8");//設置編碼
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//設置容錯等級: L M Q H
hints.put(EncodeHintType.MARGIN,1);//設置邊距
//生成文件
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,hints);
Path file = new File(path).toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
e.printStackTrace();
}
}