`
amu123456
  • 浏览: 8722 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

QR Code

    博客分类:
  • code
阅读更多
public static ByteArrayOutputStream createQrImage(String content, int width, int height, String formatName){
ByteArrayOutputStream baos = null;
try{
baos = new ByteArrayOutputStream(4096);
Hashtable<EncodeHintType, Object> hintMap = new Hashtable<EncodeHintType, Object>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hintMap.put(EncodeHintType.MARGIN, 0);
BitMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hintMap);
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++) {
for (int j = 0; j < matrixWidth; j++) {
image.setRGB(i, j, byteMatrix.get(i, j) ? 0xFF000000 : 0xFFFFFFFF);
}
}
ImageIO.write(image, formatName, baos);
}catch(Exception ex){
ex.printStackTrace();
}
return baos ;
}
/**
*content 请求地址
*/
private byte[] createQrImage(String content) throws IOException {
byte[] img = null;
ByteArrayOutputStream baos = null;
try {
baos = YQISUtils.createQrImage(content, 360, 360, "png");
img = baos.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (baos != null) {
baos.close();
}
}
return img;
}

public void genimg(){
try {
byte[] imgs = this.createQrImage("https://com.cn/forwardTemperatureLogRegPage.do");
String imgname =  "C:\\Users\\Muze Lin\\Downloads\\jiankanshenbao.jpg";
    FileImageOutputStream imageOutput = new FileImageOutputStream(new File(imgname));//打开输入流
     imageOutput.write(imgs, 0, imgs.length);//将byte写入硬盘
    imageOutput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static BitMatrix createBarcodeImageBitMatrix(String content ,int width , int heigth , Hashtable<EncodeHintType, Object> hintMap){
try{
if(hintMap == null){
hintMap = new Hashtable<EncodeHintType, Object>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 
hintMap.put(EncodeHintType.MARGIN, 1);
}
BitMatrix byteMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.CODE_128, width, heigth, hintMap);
return byteMatrix ;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics