本文整理汇总了Java中com.google.zxing.MultiFormatWriter类的典型用法代码示例。如果您正苦于以下问题:Java MultiFormatWriter类的具体用法?Java MultiFormatWriter怎么用?Java MultiFormatWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MultiFormatWriter类属于com.google.zxing包,在下文中一共展示了MultiFormatWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encodeStringToBitmap
import com.google.zxing.MultiFormatWriter; //导入依赖的package包/类
public static Bitmap encodeStringToBitmap(String contents) throws WriterException {
//Null check, just b/c
if (contents == null) {
return null;
}
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, BarcodeFormat.PDF_417, 700, 900, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例2: getQR
import com.google.zxing.MultiFormatWriter; //导入依赖的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));
}
示例3: getQRCodeImge
import com.google.zxing.MultiFormatWriter; //导入依赖的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();
}
}
}
示例4: barcode417
import com.google.zxing.MultiFormatWriter; //导入依赖的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());
}
示例5: createQRCode
import com.google.zxing.MultiFormatWriter; //导入依赖的package包/类
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例6: encode
import com.google.zxing.MultiFormatWriter; //导入依赖的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;
}
示例7: encode
import com.google.zxing.MultiFormatWriter; //导入依赖的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;
}
示例8: createImage
import com.google.zxing.MultiFormatWriter; //导入依赖的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;
}
示例9: encodeAsBitmap
import com.google.zxing.MultiFormatWriter; //导入依赖的package包/类
public Bitmap encodeAsBitmap() throws WriterException {
if (!encoded) return null;
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contents);
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
if (encoding != null) {
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
hints.put(EncodeHintType.MARGIN, 2); /* default = 4 */
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例10: encodeAsBitmap
import com.google.zxing.MultiFormatWriter; //导入依赖的package包/类
public Bitmap encodeAsBitmap() throws WriterException {
if (!encoded) return null;
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contents);
if (encoding != null) {
hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
// All are 0, or black, by default
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例11: createQRCode
import com.google.zxing.MultiFormatWriter; //导入依赖的package包/类
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例12: createQRCode
import com.google.zxing.MultiFormatWriter; //导入依赖的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;
}
示例13: Create2DCode
import com.google.zxing.MultiFormatWriter; //导入依赖的package包/类
/**
* 用字符串生成二维码
*
* @param str
* @author J!nl!n
* @return
* @throws WriterException
*/
public Bitmap Create2DCode(String str) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 480, 480, hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// 通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例14: Create2DCode
import com.google.zxing.MultiFormatWriter; //导入依赖的package包/类
/**
* 用字符串生成二维码
*
* @param str
* @author J!nl!n
* @return
* @throws WriterException
*/
public static Bitmap Create2DCode(String str) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 480, 480, hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// 通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例15: createQRCode
import com.google.zxing.MultiFormatWriter; //导入依赖的package包/类
/**
* Generate a two-dimensional code image
*
* @param content An incoming string, usually a URL
* @param widthAndHeight widthAndHeight
* @param openErrorCorrectionLevel Does fault tolerance open?
* @return A two-dimensional code image [Bitmap]
*/
public static Bitmap createQRCode(String content, int widthAndHeight, boolean openErrorCorrectionLevel) {
try {
if (TextUtils.isEmpty(content) || TextUtils.equals("null", content) || "".equals(content)) {
return null;
}
//Processing Chinese characters, if you do not need to change the source code, convert the string into ISO-8859-1 code.
Map hints = openErrorCorrectionLevel(openErrorCorrectionLevel);
BitMatrix matrix = new MultiFormatWriter().encode(new String(content.getBytes("UTF-8"), "ISO-8859-1"), BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);
matrix = updateBit(matrix, 8);
Bitmap bitmap = generateQRBitmap(matrix);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}