❶ java生成二维码如何去除白边
代码如下:
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = new QRCodeWriter().encode("生成二维码的内容",
BarcodeFormat.QR_CODE, 256, 256,hints);
//1.1去白边
int[] rec = bitMatrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (bitMatrix.get(i + rec[0], j + rec[1])) {
resMatrix.set(i, j);
}
}
}
//2
int width = resMatrix.getWidth();
int height = resMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, resMatrix.get(x, y) == true ?
Color.BLACK.getRGB():Color.WHITE.getRGB());
}
}
//3
ImageIO.write(image,"png", new File("生成二维码保存的路径"));
2
这时候生成的二维码图片就没有四周的白边了。
❷ java怎么生成带logo二维码
1、下载生成二维码所需要的jar包qrcode.jar;
2、直接上生成二维码的java代码
//需要导入的包
importjava.awt.Color;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjavax.imageio.ImageIO;
importcom.swetake.util.Qrcode;
/**
*生成二维码(QRCode)图片
*@paramcontent二维码图片的内容
*@paramimgPath生成二维码图片完整的路径
*@paramccbpath二维码图片中间的logo路径
*/
publicstaticintcreateQRCode(Stringcontent,StringimgPath,StringccbPath){
try{
QrcodeqrcodeHandler=newQrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);
//System.out.println(content);
byte[]contentBytes=content.getBytes("gb2312");
//构造一个BufferedImage对象设置宽、高
BufferedImagebufImg=newBufferedImage(140,140,BufferedImage.TYPE_INT_RGB);
Graphics2Dgs=bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0,0,140,140);
//设定图像颜色>BLACK
gs.setColor(Color.BLACK);
//设置偏移量不设置可能导致解析出错
intpixoff=2;
//输出内容>二维码
if(contentBytes.length>0&&contentBytes.length<120){
boolean[][]codeOut=qrcodeHandler.calQrcode(contentBytes);
for(inti=0;i<codeOut.length;i++){
for(intj=0;j<codeOut.length;j++){
if(codeOut[j][i]){
gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
}
}
}
}else{
System.err.println("QRCodecontentbyteslength="
+contentBytes.length+"notin[0,120].");
return-1;
}
Imageimg=ImageIO.read(newFile(ccbPath));//实例化一个Image对象。
gs.drawImage(img,55,55,30,30,null);
gs.dispose();
bufImg.flush();
//生成二维码QRCode图片
FileimgFile=newFile(imgPath);
ImageIO.write(bufImg,"png",imgFile);
}catch(Exceptione){
e.printStackTrace();
return-100;
}
return0;
}
来自网友孤独青鸟的博客
❸ 怎么用java代码把一个链接生成二维码
参考代码
import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class QRCodeEncoderTest
{
/** Creates a new instance of QRCodeEncoderTest */
public QRCodeEncoderTest()
{
}
public static void create_image(String sms_info)throws Exception{
try{
qrcode testQrcode =new qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
String testString = sms_info;
byte[] d = testString.getBytes("gbk");
System.out.println(d.length);
//BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_INT_RGB);
BufferedImage bi = new BufferedImage(98, 98,
❹ 如何使用java,servlet创建二维码
java需要Zebra Crossing(ZXing),你可以去他们网站下载,然后下面是一个例子程序:
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importnet.glxn.qrgen.QRCode;
importnet.glxn.qrgen.image.ImageType;
publicclass
Main {
publicstatic
void
main(String[] args) {
ByteArrayOutputStream
out = QRCode.from("Hello
World").to(ImageType.PNG).stream();
try{
FileOutputStream
fout = newFileOutputStream(newFile(
"C:QR_Code.JPG"));
fout.write(out.toByteArray());
fout.flush();
fout.close();
}catch(FileNotFoundException
e) {
//
Do Logging
}catch(IOException
e) {
//
Do Logging
}
}
}