『壹』 java 如何向ZeBra列印機發送字元串
可以參考下.自己寫的一個例子
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.Queue;
import com.google.zxing.WriterException;
public class QrPrint implements Printable {
public void print() throws PrinterException {
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper paper = new Paper();
double margin = 0.1; // half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2,paper.getHeight() - margin * 2);
pf.setPaper(paper);
pj.setPrintable(this, pf);
pj.print();
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if ("判斷如果列印完了就退出") {
return Printable.NO_SUCH_PAGE;
}
try {
Image eanImage = generateEANBufferedImage(content,120,20);
Graphics2D g2 = (Graphics2D) graphics;
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //設置可成像的和Y軸的距離
g2.drawImage(eanImage, 0, 80, null); //設置列印的邊距
g2.drawString("字元串:",130,20);
} catch (WriterException e) {
e.printStackTrace();
return Printable.NO_SUCH_PAGE;
}
return Printable.PAGE_EXISTS;
}
private BufferedImage generateBufferedImage(String content,int width,int height) throws WriterException {
Hashtable<EncodeHintType,String> hints = new Hashtable<EncodeHintType,String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.EAN_13, width, height, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
}
}
『貳』 java中zxing二維碼怎麼在掃描的時候跳轉到指定頁面
有沒有沒解決的?找到方法了,
@SuppressWarnings({"rawtypes","unchecked"})
privatestaticvoidcreateZxing()throwsWriterException,IOException{
intwidth=300;
inthight=300;
Stringformat="png";
Stringcontent="https://www..com";//這里要注意!!!!!!!
HashMaphints=newHashMap();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION,
+ErrorCorrectionLevel.M);//糾錯等級L,M,Q,H
hints.put(EncodeHintType.MARGIN,2);//邊距
BitMatrixbitMatrix=newMultiFormatWriter().encode(content,
+BarcodeFormat.QR_CODE,width,hight,hints);
Filefile=newFile("D:/imag.png");
MatrixToImageWriter.writeToFile(bitMatrix,format,file);
}
/*你需要跳轉的頁面前面加上http協議,掃描的軟體比方說:微信,它會直接識別http
協議*/
『叄』 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源碼 zxing
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
}