本文整理匯總了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);
}
}