本文整理匯總了Java中com.google.zxing.client.j2se.MatrixToImageWriter類的典型用法代碼示例。如果您正苦於以下問題:Java MatrixToImageWriter類的具體用法?Java MatrixToImageWriter怎麽用?Java MatrixToImageWriter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MatrixToImageWriter類屬於com.google.zxing.client.j2se包,在下文中一共展示了MatrixToImageWriter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getQR
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的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.client.j2se.MatrixToImageWriter; //導入依賴的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: barcode417
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
@RequestMapping(value = "/pdf417/{id}", method = RequestMethod.GET, produces = IMAGE_PNG)
public void barcode417(@PathVariable("id") String id, HttpServletResponse response) throws IOException, WriterException {
if (!CLIENT_ID_PATTERN.matcher(id).matches()) {
throw new InputValidationException("Invalid clientId for barcode [" + id + "]");
}
response.setContentType(IMAGE_PNG);
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {{
put(EncodeHintType.MARGIN, MARGIN_PIXELS);
put(EncodeHintType.ERROR_CORRECTION, 2);
put(EncodeHintType.PDF417_COMPACT, true);
put(EncodeHintType.PDF417_COMPACTION, Compaction.TEXT);
}};
BitMatrix matrix = new MultiFormatWriter().encode(id, BarcodeFormat.PDF_417, BARCODE_WIDTH, BARCODE_HEIGHT, hints);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix);
BufferedImage croppedImage = cropImageWorkaroundDueToZxingBug(bufferedImage);
ImageIO.write(croppedImage, "PNG", response.getOutputStream());
}
示例4: encode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的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: encode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
/**
* 條形碼編碼
*/
public static BufferedImage encode(String contents, int width, int height) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
try {
// 原為13位Long型數字(BarcodeFormat.EAN_13),現為128位
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, codeWidth, height, null);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例6: createQrcode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
public static String createQrcode(String dir, String _text) {
String qrcodeFilePath = "";
try {
int qrcodeWidth = 300;
int qrcodeHeight = 300;
String qrcodeFormat = "png";
HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(_text, BarcodeFormat.QR_CODE, qrcodeWidth,
qrcodeHeight, hints);
BufferedImage image = new BufferedImage(qrcodeWidth, qrcodeHeight, BufferedImage.TYPE_INT_RGB);
File qrcodeFile = new File(dir + "/" + UUID.randomUUID().toString() + "." + qrcodeFormat);
ImageIO.write(image, qrcodeFormat, qrcodeFile);
MatrixToImageWriter.writeToPath(bitMatrix, qrcodeFormat, qrcodeFile.toPath());
qrcodeFilePath = qrcodeFile.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
}
return qrcodeFilePath;
}
示例7: barCode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
public static void barCode(String contents,String imgPath,int width, int height) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.CODE_128, codeWidth, height, null);
bitMatrix = deleteWhite(bitMatrix);
MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
}catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
示例8: encode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
/**
* 生成條形二維碼
*/
public static byte[] encode(String contents, int width, int height, String imgPath) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
byte[] buf = null;
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.CODE_128, codeWidth, height, null);
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, FORMAT_NAME, out);
out.close();
buf = out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return buf;
}
示例9: createImage
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的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();
}
}
示例10: encode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
/**
* 條形碼編碼
* <p>
* String imgPath = "target/zxing.png";
* String contents = "6923450657713";
* int width = 105, height = 50;
* ZxingHelper.encode(contents, width, height, imgPath);
*
* @param contents 內容
* @param width 寬度
* @param height 高度
* @param imgPath 生成圖片路徑
*/
public static void encode(String contents, int width, int height, String imgPath) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.EAN_13, codeWidth, height, null);
MatrixToImageWriter
.writeToPath(bitMatrix, "png", new File(imgPath).toPath());
} catch (Exception e) {
LOGGER.error("生成條形碼錯誤", e);
}
}
示例11: encode2
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
/**
* 二維碼編碼
* <p>
* String imgPath2 = "target/zxing2.png";
* String contents2 = "Hello Zxing!";
* int width2 = 300, height2 = 300;
* ZxingHelper.encode2(contents2, width2, height2, imgPath2);
*
* @param contents 內容
* @param width 寬度
* @param height 高度
* @param imgPath 生成圖片路徑
*/
public static void encode2(String contents, int width, int height, String imgPath) {
EnumMap<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
// 指定糾錯等級
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定編碼格式
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter
.writeToPath(bitMatrix, "png", new File(imgPath).toPath());
} catch (Exception e) {
LOGGER.error("生成二維碼錯誤", e);
}
}
示例12: writeInfoToJpgBuffImg
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
/**
* 二維碼信息寫成JPG BufferedImage
*
* @param content
* @param width
* @param height
* @return
*/
public static BufferedImage writeInfoToJpgBuffImg(String content, int width, int height) {
if (width < 250) {
width = 250;
}
if (height < 250) {
height = 250;
}
BufferedImage re = null;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
BitMatrix bitMatrix = multiFormatWriter.encode(content,
BarcodeFormat.QR_CODE, width, height, hints);
re = MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (Exception e) {
e.printStackTrace();
}
return re;
}
示例13: createQRCode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
/**二維碼的生成*/
private void createQRCode(HttpServletResponse response){
/**設置二維碼的參數*/
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 內容所使用編碼
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 留白區域大小
hints.put(EncodeHintType.MARGIN, margin);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
// 輸出二維碼
MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
} catch (Exception e) {
System.out.println("生成二維碼異常!"+e.toString());
}
}
示例14: createBarCode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
/**條形碼的生成*/
private void createBarCode(HttpServletResponse response){
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.CODE_128, codeWidth, height, null);
// 條形碼
MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
} catch (Exception e) {
System.out.println("生成條形碼異常!"+e.toString());
}
}
示例15: createQrcode
import com.google.zxing.client.j2se.MatrixToImageWriter; //導入依賴的package包/類
/**
* 二維碼生成 ,儲存地址qrcodeFilePath
*
* @param text 二維碼內容
* @return Base64Code String
*/
@SuppressWarnings("restriction")
public String createQrcode(String text){
String Imgencode="";
byte[] imageByte=null;
String formatName="PNG";
try {
//
int qrcodeWidth = 300;
int qrcodeHeight = 300;
HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
ImageIO.write(bufferedImage, formatName, out);
imageByte=out.toByteArray();
Imgencode = new sun.misc.BASE64Encoder().encode(imageByte);
} catch (Exception e) {
e.printStackTrace();
}
return Imgencode;
}