思考
最初の永久マイクロチャネルの二次元コードを取得し、
ステップ2次元コード分析情報、
第三工程:新しい2次元コードの合成
ステップ4:二次元コードは、OSSサーバにアップロード
コード
String result = HttpUtils.doPost("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+matoken.getToken(),JSONObject.toJSONString(jsonObject));
String ticket = JSONObject.parseObject(result).getString("ticket");
String qrContent = QrCodeUtils.decode("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket);
url=QrCodeUtils.encode(qrContent,userDto.getIconUrl(),true);
デコード方法
public static String decode(String imgUrl) {
String resultStr="";
try {
URL url = new URL(imgUrl);
BufferedImage image = ImageIO.read(url);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, “UTF-8”);
result = new MultiFormatReader().decode(bitmap, hints);
resultStr = result.getText();
}catch (Exception e){
e.printStackTrace();
}
return resultStr;
}
エンコード方法
public static String encode(String content, String imgUrl, boolean needCompress) {
String url = "";
try {
BufferedImage image = QrCodeUtils.createImage(content, imgUrl,needCompress);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image,"png", out);
url= Upload.upLoadOssByBytes(out.toByteArray(),"png");
}catch (Exception e){
e.printStackTrace();
}
return url;
}
二次元コードの合成
private static BufferedImage createImage(String content, String imgPath,boolean needCompress) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 4);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_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 (imgPath == null || "".equals(imgPath)) {
return image;
}
URL url = new URL(imgUrl);
Image src = ImageIO.read(url);
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) {
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height,Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
return image;
}
アップロードOSSサーバ
public static String upLoadOssByBytes(byte[] bytes,String suffixName){
String name = UUID.randomUUID().toString();
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
ossClient.putObject("ssp-img", name+"."+suffixName, new ByteArrayInputStream(bytes));
ossClient.shutdown();
}catch (Exception e){
ossClient.shutdown();
}
return ossUrl+"/"+name+"."+suffixName;
}
終わり!!!!