『壹』 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);
}
}
}