生成二维码导入Ireport
图片直接转换为输入流
#
标题
一、生成二维码并且将图片转换成为输入流
代码如下(示例):
public BufferedImage addLogo(BufferedImage bufferedImage) {
Graphics2D graphics2D = bufferedImage.createGraphics();
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
try {
//添加二维码 中心的Logo 也可以手动设定Logo位置
BufferedImage logo = ImageIO.read(new File("D:\\阿葵.jpg"));
graphics2D.drawImage(logo, width / 5 * 2, height / 5 * 2, width / 5, height / 5, null);
BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL);
graphics2D.setStroke(stroke);// 设置笔画对象
//指定弧度的圆角矩形
RoundRectangle2D.Float round = new RoundRectangle2D.Float(width / 5 * 2, height / 5 * 2, width / 5, height / 5, 20, 20);
graphics2D.setColor(Color.white);
graphics2D.draw(round);// 绘制圆弧矩形
//设置logo 有一道灰色边框
BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
graphics2D.setStroke(stroke2);// 设置笔画对象
RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(width / 5 * 2 + 2, height / 5 * 2 + 2, width / 5 - 4, height / 5 - 4, 20, 20);
graphics2D.setColor(new Color(128, 128, 128));
graphics2D.draw(round2);// 绘制圆弧矩形
graphics2D.dispose();
bufferedImage.flush();
} catch (IOException e) {
e.printStackTrace();
}
return bufferedImage;
}
public InputStream createQrCode(int width, int height) {
boolean flag = false;
InputStream inputStream = null;
if (width >= 0 && width <= 500 && height >= 0 && height <= 500) {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeVersion(4);//设置qrcode版本可选1~40
qrcode.setQrcodeEncodeMode('B');//设置编码类型B表示二进制编码
qrcode.setQrcodeErrorCorrect('M');//设置纠错类型M,表示15
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);//设置bufferImage宽高,色域
Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setBackground(Color.white);
graphics2D.setColor(Color.BLACK);
graphics2D.clearRect(0, 0, width, height);
int prefix = 20;//设置偏移量
try {
byte[] bytes = content.getBytes("utf-8");
if (bytes.length > 0 && bytes.length < 1000) {
boolean[][] booleans = qrcode.calQrcode(bytes);
for (int i = 0; i < booleans.length; i++) {
for (int j = 0; j < booleans.length; j++) {
if (booleans[i][j]) {
//写入图片
graphics2D.fillRect(i * 10 + prefix, j * 10 + prefix, 10, 10);
}
}
}
}
graphics2D.dispose();//关闭画图
bufferedImage.flush();//刷新
//addLogo(bufferedImage);
//ImageIO.write(bufferedImage, "png", img);
//ByteArrayOutputStream 是可以讲文件接收到成为一个outputStream
ByteArrayOutputStream os = new ByteArrayOutputStream();
//ImageIo.write可以将文件转换成为一个输出流
ImageIO.write(bufferedImage, "png",os);
//ByteArrayInputStream 可以通过ByteArrayOutputStream os.toByteArray()输出的比特数组生成一个输出流 输出流->输入流
inputStream=new ByteArrayInputStream(os.toByteArray());
return inputStream;
} catch (IOException e) {
e.printStackTrace();
}
}
return inputStream;
}
2.读入数据
代码如下(示例):
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
该处使用的url网络请求的数据。