本文整理汇总了Java中com.google.zxing.client.j2se.MatrixToImageWriter.writeToStream方法的典型用法代码示例。如果您正苦于以下问题:Java MatrixToImageWriter.writeToStream方法的具体用法?Java MatrixToImageWriter.writeToStream怎么用?Java MatrixToImageWriter.writeToStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.zxing.client.j2se.MatrixToImageWriter
的用法示例。
在下文中一共展示了MatrixToImageWriter.writeToStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
}
}
示例2: 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());
}
}
示例3: 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));
}
示例4: 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());
}
}
示例5: printImage
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
/**
* Writes the image file to the output stream.
*
* @param graph the object graph
* @param exchange the camel exchange
* @param stream the output stream
*/
private void printImage(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
final String payload = ExchangeHelper
.convertToMandatoryType(exchange, String.class, graph);
final MultiFormatWriter writer = new MultiFormatWriter();
// set values
final String type = this.params.getType().toString();
// create code image
final BitMatrix matrix = writer.encode(
payload,
this.params.getFormat(),
this.params.getWidth(),
this.params.getHeight(),
writerHintMap);
// write image back to stream
MatrixToImageWriter.writeToStream(matrix, type, stream);
}
示例6: qrcode
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
@RequireAnyAuthority
@RequestMapping(value = "/qr", method = RequestMethod.GET)
public void qrcode(HttpServletResponse response,
@AuthenticationPrincipal JpaUserDetails jpaUserDetails)
throws WriterException, IOException {
User user = jpaUserDetails.getUser(this.jpaQueryFactory);
if (user != null && StringUtils.hasText(user.getSecret())) {
response.setContentType("image/png");
String contents = "otpauth://totp/" + user.getEmail() + "?secret="
+ user.getSecret() + "&issuer=" + this.appName;
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = writer.encode(contents, BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToStream(matrix, "PNG", response.getOutputStream());
response.getOutputStream().flush();
}
}
示例7: qrcode
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
@RequireAnyAuthority
@RequestMapping(value = "/qr", method = RequestMethod.GET)
public void qrcode(HttpServletResponse response,
@AuthenticationPrincipal MongoUserDetails userDetails)
throws WriterException, IOException {
User user = userDetails.getUser(this.mongoDb);
if (user != null && StringUtils.hasText(user.getSecret())) {
response.setContentType("image/png");
String contents = "otpauth://totp/" + user.getEmail() + "?secret="
+ user.getSecret() + "&issuer=" + this.appName;
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = writer.encode(contents, BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToStream(matrix, "PNG", response.getOutputStream());
response.getOutputStream().flush();
}
}
示例8: renderCode
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
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);
}
}
示例9: qrcode
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
@RequestMapping(value = "/qrcode/{username}.png", method = RequestMethod.GET)
public void qrcode(HttpServletResponse response,
@PathVariable("username") String username)
throws WriterException, IOException {
User user = this.userRepository.findByUserName(username);
if (user != null) {
response.setContentType("image/png");
String contents = "otpauth://totp/" + username + ":" + user.getEmail()
+ "?secret=" + user.getSecret() + "&issuer=SpringSecurityTOTP";
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = writer.encode(contents, BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToStream(matrix, "PNG", response.getOutputStream());
response.getOutputStream().flush();
}
}
示例10: createQRCode
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
public static void createQRCode(String barCodeData, String filePath, int height, int width)
throws WriterException, IOException {
BitMatrix matrix = new MultiFormatWriter().encode(barCodeData, BarcodeFormat.QR_CODE,
width, height);
try (FileOutputStream out = new FileOutputStream(filePath)) {
MatrixToImageWriter.writeToStream(matrix, "png", out);
}
}
示例11: generateQrCodeForUri
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
private byte[] generateQrCodeForUri(String uri) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BitMatrix matrix = new QRCodeWriter().encode(uri, BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToStream(matrix, "PNG", stream);
return stream.toByteArray();
} catch (IOException | WriterException e) {
// Given that this operation is entirely in memory, any such exceptions are indicative of bad input.
throw new IllegalArgumentException("Invalid URI", e);
}
}
示例12: doWrite
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
private void doWrite(OutputStream os, Path path) throws IOException {
try {
Writer writer = new MultiFormatWriter();
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
hints.put(EncodeHintType.MARGIN, Integer.valueOf(margin));
hints.put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.forBits(errorCorrectionLevel.getBits()));
BitMatrix matrix = writer.encode(uri.toUriString(), BarcodeFormat.QR_CODE, width, height, hints);
if (os != null) {
MatrixToImageWriter.writeToStream(matrix, imageFormatName, os);
}
else {
MatrixToImageWriter.writeToPath(matrix, imageFormatName, path);
}
} catch (WriterException e) {
throw new IOException(e);
}
}
示例13: getQRCode
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
@Override
public Response getQRCode(final String url) throws IOException {
final ByteArrayOutputStream buf = new ByteArrayOutputStream();
final Writer writer = new QRCodeWriter();
final BitMatrix matrix;
try {
matrix = writer.encode(url, BarcodeFormat.QR_CODE, 100, 100);
} catch (WriterException e) {
throw new ClapException(e);
}
MatrixToImageWriter.writeToStream(matrix, "PNG", buf);
final byte[] bytes = buf.toByteArray();
LOGGER.debug("qrcode for " + url + " generated");
return returnImage(new ByteArrayInputStream(bytes), "qrcode.png");
}
示例14: encode
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
/**
* 生成二维码图片
* @param content 二维码中包含的字符串信息
* @param path 生成的二维码图片保存路径
* @param length 生成的图片长宽
* @param encodee_mode 编码方式
* @throws WriterException
* @throws IOException
*/
public static void encode(String content, String path, Integer length, String encodee_mode) throws WriterException, IOException {
if (length == null) {
length = 300;
}
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, encodee_mode);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, length, length, hints);
//MatrixToImageWriter.writeToFile(matrix, "png", new File(path));
MatrixToImageWriter.writeToStream(matrix, "png", new FileOutputStream(new File(path)));
}
示例15: createQRCode
import com.google.zxing.client.j2se.MatrixToImageWriter; //导入方法依赖的package包/类
public static byte[] createQRCode(String text) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Map<EncodeHintType, Object> hintMap = new EnumMap<>(EncodeHintType.class);
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix matrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 200, 200, hintMap);
MatrixToImageWriter.writeToStream(matrix, "png", baos);
return baos.toByteArray();
} catch (WriterException | IOException e) {
throw new IllegalStateException(e);
}
}