package com
.enation
.app
.javashop
.framework
.util
;
import com
.google
.gson
.Gson
;
import com
.google
.zxing
.BarcodeFormat
;
import com
.google
.zxing
.EncodeHintType
;
import com
.google
.zxing
.MultiFormatWriter
;
import com
.google
.zxing
.WriterException
;
import com
.google
.zxing
.client
.j2se
.MatrixToImageWriter
;
import com
.google
.zxing
.common
.BitMatrix
;
import com
.google
.zxing
.qrcode
.decoder
.ErrorCorrectionLevel
;
import lombok
.SneakyThrows
;
import javax
.imageio
.ImageIO
;
import java
.awt
.*;
import java
.awt
.geom
.RoundRectangle2D
;
import java
.awt
.image
.BufferedImage
;
import java
.io
.*;
import java
.net
.URL;
import java
.util
.HashMap
;
import java
.util
.Map
;
public class CodeImageUtil {
public static final int
WIDTH = 300;
public static final int
HEIGHT = 300;
public static final String
FORMAT = "png";
public static final Map
<EncodeHintType
, Object
> HINTS = new HashMap<EncodeHintType
, Object
>();
static {
HINTS.put(EncodeHintType
.CHARACTER_SET, "utf-8");
HINTS.put(EncodeHintType
.ERROR_CORRECTION, ErrorCorrectionLevel
.H);
HINTS.put(EncodeHintType
.MARGIN, 2);
}
public static void writeToStream(Object object
, OutputStream stream
,
Integer width
, Integer height
) throws WriterException
, IOException
{
if(width
==null){
width
=WIDTH;
}
if(height
==null){
height
=HEIGHT;
}
Gson gson
= new Gson();
String json
= gson
.toJson(object
);
BitMatrix bitMatrix
= new MultiFormatWriter().encode(json
,
BarcodeFormat
.QR_CODE, width
, height
, HINTS);
MatrixToImageWriter
.writeToStream(bitMatrix
, FORMAT, stream
);
}
@SneakyThrows
public static InputStream
write(InputStream
in){
BufferedImage bufferedImage
= ImageIO
.read(in);
Graphics2D graphics
= bufferedImage
.createGraphics();
String imageUrl
= "图片地址";
URL url
= new URL(imageUrl
);
InputStream inputStream
= url
.openStream();
BufferedImage logo
= ImageIO
.read(inputStream
);
int logoWidth
= logo
.getWidth() > bufferedImage
.getWidth()*2 /10 ? (bufferedImage
.getWidth()*2 /10) : logo
.getWidth();
int logoHeight
= logo
.getHeight() > bufferedImage
.getHeight()*2 /10 ? (bufferedImage
.getHeight()*2 /10) : logo
.getHeight();
int x
= (bufferedImage
.getWidth() - logoWidth
) / 2;
int y
= (bufferedImage
.getHeight() - logoHeight
) / 2;
graphics
.drawImage(logo
,x
,y
,logoWidth
,logoHeight
,null);
graphics
.drawRoundRect(x
,y
,logoWidth
,logoHeight
,15,15);
graphics
.setStroke(new BasicStroke(2));
graphics
.setColor(Color
.WHITE);
graphics
.drawRect(x
,y
,logoWidth
,logoHeight
);
graphics
.dispose();
logo
.flush();
bufferedImage
.flush();
ByteArrayOutputStream os
= new ByteArrayOutputStream();
ImageIO
.write(bufferedImage
, "jpg" ,os
);
in = new ByteArrayInputStream(os
.toByteArray());
return in;
}
}
ByteArrayOutputStream out
= new ByteArrayOutputStream();
String qrcode
="http://m.buyer.gdynyp.com/goods/834?channelid="+providerDO
.getFindCode()+"";
CodeImageUtil
.writeToStreamString(qrcode
, out
, 366, 366);
InputStream
in = new ByteArrayInputStream(out
.toByteArray());
in = CodeImageUtil
.write(in);
转载请注明原文地址: https://lol.8miu.com/read-33172.html