⑴ java如何用代码生成二维码
引用spire.barcode.jar包
//创建BarcodeSettings对象
BarcodeSettingssettings=newBarcodeSettings();
//设置条码类型为
QR二维码settings.setType(BarCodeType.QR_Code);
//设置二维码数据
settings.setData("Hello123456789");
//设置二维码显示数据
settings.setData2D("Hello123456789");
//设置数据类型
settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);
//设置二维码模型宽度
settings.setX(1.0f);
//设置二维码纠错级别settings.setQRCodeECL(QRCodeECL.H);
//创建BarCodeGenerator实例
=newBarCodeGenerator(settings);
//根据settings生成图像数据,保存至BufferedImage
BufferedImagebufferedImage=barCodeGenerator.generateImage();
//将图片数据保存为PNG格式
ImageIO.write(bufferedImage,"png",newFile("QRCode.png"));
⑵ java 二维码图片转二进制存进数据库 使用什么技术可以实现呢
数据库使用blob类型
图片使用java io流操作,将图片转换为byte[],存入mysql中
⑶ java 生成二维码后如何给该二维码添加信息
java可使用zxing生成二维码并为其添加信息。
以下是详细步骤:
1、创建MatrixToImageWriter类
importcom.google.zxing.common.BitMatrix;
importjavax.imageio.ImageIO;
importjava.io.File;
importjava.io.OutputStream;
importjava.io.IOException;
importjava.awt.image.BufferedImage;
{
privatestaticfinalintBLACK=0xFF000000;
privatestaticfinalintWHITE=0xFFFFFFFF;
privateMatrixToImageWriter(){}
(BitMatrixmatrix){
intwidth=matrix.getWidth();
intheight=matrix.getHeight();
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(intx=0;x<width;x++){
for(inty=0;y<height;y++){
image.setRGB(x,y,matrix.get(x,y)?BLACK:WHITE);
}
}
returnimage;
}
publicstaticvoidwriteToFile(BitMatrixmatrix,Stringformat,Filefile)
throwsIOException{
BufferedImageimage=toBufferedImage(matrix);
if(!ImageIO.write(image,format,file)){
thrownewIOException("Couldnotwriteanimageofformat"+format+"to"+file);
}
}
publicstaticvoidwriteToStream(BitMatrixmatrix,Stringformat,OutputStreamstream)
throwsIOException{
BufferedImageimage=toBufferedImage(matrix);
if(!ImageIO.write(image,format,stream)){
thrownewIOException("Couldnotwriteanimageofformat"+format);
}
}
}
2、生成二维码并添加信息
importjava.io.File;
importjava.util.Hashtable;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.MultiFormatWriter;
importcom.google.zxing.WriterException;
importcom.google.zxing.common.BitMatrix;
publicclassTest{
/**
*@paramargs
*@throwsException
*/
publicstaticvoidmain(String[]args)throwsException{
Stringtext="http://www..com";
intwidth=300;
intheight=300;
//二维码的图片格式
Stringformat="gif";
Hashtablehints=newHashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
BitMatrixbitMatrix=newMultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE,width,height,hints);
//生成二维码
FileoutputFile=newFile("d:"+File.separator+"new.gif");
MatrixToImageWriter.writeToFile(bitMatrix,format,outputFile);
}
}
⑷ java怎么生成二维码
1: 使用SwetakeQRCode在Java项目中生成二维码
这个是日本人写的,生成的是我们常见的方形的二维码
可以用中文
2: 使用BarCode4j生成条形码和二维码
barcode4j是使用datamatrix的二维码生成算法,为支持qr的算法
datamatrix是欧美的标准,qr为日本的标准,
barcode4j一般生成出来是长方形的
3:zxing
zxing 这个是google的
⑸ java 如何完成二维码的制作
参考以下代码:
//创建BarcodeSettings实例
BarcodeSettingssettings=newBarcodeSettings();
//设置条码类型为QR二维码
settings.setType(BarCodeType.QR_Code);
//设置二维码数据
settings.setData("Hello123456789");
//设置二维码显示数据
settings.setData2D("Hello123456789");
//设置数据类型
settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);
//设置二维码模型宽度
settings.setX(1.0f);
//设置二维码纠错级别
settings.setQRCodeECL(QRCodeECL.H);
//创建BarCodeGenerator实例
=newBarCodeGenerator(settings);
//根据settings生成图像数据,保存至BufferedImage实例
BufferedImagebufferedImage=barCodeGenerator.generateImage();
//保存为PNG图片
ImageIO.write(bufferedImage,"png",newFile("QRCode.png"));
System.out.println("Complete!");
需要引用Spire.Barcode for java
原文:Java 生成二维码
⑹ 怎么使用java生成DataMatrix格式的二维码
参考:
import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class CreateDataMatrix {
public static void main(String[] args) throws Exception {
//生成BarcodeSettings实例
BarcodeSettings settings = new BarcodeSettings();
//设置条形码类型为DataMatrix
settings.setType(BarCodeType.Data_Matrix);
//设置条形码模型宽度
settings.setX(1.5f);
//设置数据和显示文本
settings.setData("ABC 123456789ABC 123456789ABC 123456789");
settings.setData2D("ABC 123456789ABC 123456789ABC 123456789");
//创建BarCodeGenerator实例
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
//根据settings生成图像数据,保存至BufferedImage实例
BufferedImage bufferedImage = barCodeGenerator.generateImage();
//保存为PNG图片
ImageIO.write(bufferedImage, "png", new File("DataMatrix.png"));
System.out.println("Complete!");
}
}
用到了spire.barcode for java库
⑺ 如何用java生成二维码
packagecommon;
importjava.awt.Color;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.Map;
importjavax.imageio.ImageIO;
importjp.sourceforge.qrcode.QRCodeDecoder;
importjp.sourceforge.qrcode.exception.DecodingFailedException;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.Binarizer;
importcom.google.zxing.BinaryBitmap;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.LuminanceSource;
importcom.google.zxing.MultiFormatReader;
importcom.google.zxing.MultiFormatWriter;
importcom.google.zxing.NotFoundException;
importcom.google.zxing.WriterException;
importcom.google.zxing.common.BitMatrix;
importcom.google.zxing.common.HybridBinarizer;
importcom.swetake.util.Qrcode;
/**
*二维码生成工具类
*@authorCloud
*@data2016-12-15
*QRCode
*/
publicclassQRCodeUtil{
//二维码颜色
privatestaticfinalintBLACK=0xFF000000;
//二维码颜色
privatestaticfinalintWHITE=0xFFFFFFFF;
/**
*<spanstyle="font-size:18px;font-weight:blod;">ZXing方式生成二维码</span>
*@paramtext<ahref="javascript:void();">二维码内容</a>
*@paramwidth二维码宽
*@paramheight二维码高
*@paramoutPutPath二维码生成保存路径
*@paramimageType二维码生成格式
*/
(Stringtext,intwidth,intheight,StringoutPutPath,StringimageType){
Map<EncodeHintType,String>his=newHashMap<EncodeHintType,String>();
//设置编码字符集
his.put(EncodeHintType.CHARACTER_SET,"utf-8");
try{
//1、生成二维码
BitMatrixencode=newMultiFormatWriter().encode(text,BarcodeFormat.QR_CODE,width,height,his);
//2、获取二维码宽高
intcodeWidth=encode.getWidth();
intcodeHeight=encode.getHeight();
//3、将二维码放入缓冲流
BufferedImageimage=newBufferedImage(codeWidth,codeHeight,BufferedImage.TYPE_INT_RGB);
for(inti=0;i<codeWidth;i++){
for(intj=0;j<codeHeight;j++){
//4、循环将二维码内容定入图片
image.setRGB(i,j,encode.get(i,j)?BLACK:WHITE);
}
}
FileoutPutImage=newFile(outPutPath);
//如果图片不存在创建图片
if(!outPutImage.exists())
outPutImage.createNewFile();
//5、将二维码写入图片
ImageIO.write(image,imageType,outPutImage);
}catch(WriterExceptione){
e.printStackTrace();
System.out.println("二维码生成失败");
}catch(IOExceptione){
e.printStackTrace();
System.out.println("生成二维码图片失败");
}
}
/**
*<spanstyle="font-size:18px;font-weight:blod;">二维码解析</span>
*@paramanalyzePath二维码路径
*@return
*@throwsIOException
*/
@SuppressWarnings({"rawtypes","unchecked"})
(StringanalyzePath)throwsException{
MultiFormatReaderformatReader=newMultiFormatReader();
Objectresult=null;
try{
Filefile=newFile(analyzePath);
if(!file.exists())
{
return"二维码不存在";
}
BufferedImageimage=ImageIO.read(file);
LuminanceSourcesource=newLuminanceSourceUtil(image);
Binarizerbinarizer=newHybridBinarizer(source);
BinaryBitmapbinaryBitmap=newBinaryBitmap(binarizer);
Maphints=newHashMap();
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
result=formatReader.decode(binaryBitmap,hints);
}catch(NotFoundExceptione){
e.printStackTrace();
}
returnresult;
}
/**
*<spanstyle="font-size:18px;font-weight:blod;">QRCode方式生成二维码</span>
*@paramcontent二维码内容
*@paramimgPath二维码生成路径
*@paramversion二维码版本
*@paramisFlag是否生成Logo图片为NULL不生成
*/
publicstaticvoidQRCodeCreate(Stringcontent,StringimgPath,intversion,StringlogoPath){
try{
QrcodeqrcodeHandler=newQrcode();
//设置二维码排错率,可选L(7%)M(15%)Q(25%)H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
//N代表数字,A代表字符a-Z,B代表其他字符
qrcodeHandler.setQrcodeEncodeMode('B');
//版本1为21*21矩阵,版本每增1,二维码的两个边长都增4;所以版本7为45*45的矩阵;最高版本为是40,是177*177的矩阵
qrcodeHandler.setQrcodeVersion(version);
//根据版本计算尺寸
intimgSize=67+12*(version-1);
byte[]contentBytes=content.getBytes("gb2312");
BufferedImagebufImg=newBufferedImage(imgSize,imgSize,BufferedImage.TYPE_INT_RGB);
Graphics2Dgs=bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0,0,imgSize,imgSize);
//设定图像颜色>BLACK
gs.setColor(Color.BLACK);
//设置偏移量不设置可能导致解析出错
intpixoff=2;
//输出内容>二维码
if(contentBytes.length>0&&contentBytes.length<130){
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,130].");
}
/*判断是否需要添加logo图片*/
if(logoPath!=null){
Fileicon=newFile(logoPath);
if(icon.exists()){
intwidth_4=imgSize/4;
intwidth_8=width_4/2;
intheight_4=imgSize/4;
intheight_8=height_4/2;
Imageimg=ImageIO.read(icon);
gs.drawImage(img,width_4+width_8,height_4+height_8,width_4,height_4,null);
gs.dispose();
bufImg.flush();
}else{
System.out.println("Error:login图片还在在!");
}
}
gs.dispose();
bufImg.flush();
//创建二维码文件
FileimgFile=newFile(imgPath);
if(!imgFile.exists())
imgFile.createNewFile();
//根据生成图片获取图片
StringimgType=imgPath.substring(imgPath.lastIndexOf(".")+1,imgPath.length());
//生成二维码QRCode图片
ImageIO.write(bufImg,imgType,imgFile);
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*<spanstyle="font-size:18px;font-weight:blod;">QRCode二维码解析</span>
*@paramcodePath二维码路径
*@return解析结果
*/
(StringcodePath){
FileimageFile=newFile(codePath);
BufferedImagebufImg=null;
StringdecodedData=null;
try{
if(!imageFile.exists())
return"二维码不存在";
bufImg=ImageIO.read(imageFile);
QRCodeDecoderdecoder=newQRCodeDecoder();
decodedData=newString(decoder.decode(newImageUtil(bufImg)),"gb2312");
}catch(IOExceptione){
System.out.println("Error:"+e.getMessage());
e.printStackTrace();
}catch(DecodingFailedExceptiondfe){
System.out.println("Error:"+dfe.getMessage());
dfe.printStackTrace();
}
returndecodedData;
}
}
3、最后贴测试代码:
packagetest;
importjava.awt.image.BufferedImage;
importjava.io.InputStream;
importjava.net.URL;
importjavax.imageio.ImageIO;
importcommon.ImageUtil;
importcommon.QRCodeUtil;
importjp.sourceforge.qrcode.QRCodeDecoder;
/**
*二维码生成测试类
*@authorCloud
*@data2016-11-21
*QRCodeTest
*/
publicclassQRCodeTest{
publicstaticvoidmain(String[]args)throwsException{
/**
*QRcode二维码生成测试
*QRCodeUtil.QRCodeCreate("http://blog.csdn.net/u014266877","E://qrcode.jpg",15,"E://icon.png");
*/
/**
*QRcode二维码解析测试
*StringqrcodeAnalyze=QRCodeUtil.QRCodeAnalyze("E://qrcode.jpg");
*/
/**
*ZXingCode二维码生成测试
*QRCodeUtil.zxingCodeCreate("http://blog.csdn.net/u014266877",300,300,"E://zxingcode.jpg","jpg");
*/
/**
*ZxingCode二维码解析
*StringzxingAnalyze=QRCodeUtil.zxingCodeAnalyze("E://zxingcode.jpg").toString();
*/
System.out.println("success");
}
}
⑻ java 生成二维码图片 到doc
https://www.cnblogs.com/Joanna-Yan/p/5280272.html
图片和文字也差不多吧
⑼ java中怎么把图片啊。音频之类的转为二维码。和解码。求实例和注释。或资料。谢谢
按照我的想法,二维码本身是不具备存储图片的能力的,当然,特殊的情况也有,但是最直接和最方便(最没技术含量)的方式,是把图片存储在服务器上,每次扫描就去服务器,根据二维码扫描出来的信息加载图片