求用java代码控制斑马条码打印机打印二维码商标的例子程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求用java代码控制斑马条码打印机打印二维码商标的例子程序相关的知识,希望对你有一定的参考价值。
可以参考下.自己写的一个例子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()); //设置可成像的X和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);
参考技术A 想方便一些,得用JNI,具体的没法提供了。
听说:TSC的有实例与操作说明的
斑马打印机
(待测试)
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace _01_UserTestUI
{
/*条码打印命令说明
^XA //条码打印指令开始
^MD30 //设置色带颜色的深度, 取值范围从-30到30
^LH60,10 //设置条码纸的边距
^FO20,10 //设置条码左上角的位置
^ACN,18,10 //设置字体
^BY1.4,3,50 //设置条码样式。1.4是条码的缩放级别,3是条码中粗细柱的比例, 50是条码高度
^BC,,Y,N //打印code128的指令
^ FD12345678^FS //设置要打印的内容, ^FD是要打印的条码内容^FS表示换行
^ XZ //条码打印指令结束
*/ //上面的指令会打印12345678的CODE128的条码
/// <summary>
/// 属性类
/// </summary>
public class CreatData
{
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[DllImport("kernel32.dll")]
private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject);
private int iHandle;
public bool Open()
{
iHandle = CreateFile("LPT1:", (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
public bool Write(string Mystring)
{
if (iHandle != -1)
{
int i;
OVERLAPPED x;
byte[] mybyte = Encoding.Default.GetBytes(Mystring);
return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
}
else
{
throw new Exception("LPT1端口未打开!");
}
}
public bool Close()
{
return CloseHandle(iHandle);
}
[DllImport("fnthex32.dll")]
public static extern int GETFONTHEX(
string BarcodeText,
string FontName,
string FileName,
int Orient,
int Height,
int Width,
int IsBold,
int IsItalic,
StringBuilder ReturnBarcodeCMD);
string sBarCodeCMD = ""; //条码打印命令
StringBuilder sb1 = new StringBuilder(10240);
int i1;
//i1 = GETFONTHEX("测试","宋体","temp1",0,30,20,0,0, sb1);
//sBarCodeCMD = sb1.ToString() + "^XA^MD30^LH20,20^FO20,20^XGtemp1,1,1^FS^XZ";
}
}
{
/*条码打印命令说明
^XA //条码打印指令开始
^MD30 //设置色带颜色的深度, 取值范围从-30到30
^LH60,10 //设置条码纸的边距
^FO20,10 //设置条码左上角的位置
^ACN,18,10 //设置字体
^BY1.4,3,50 //设置条码样式。1.4是条码的缩放级别,3是条码中粗细柱的比例, 50是条码高度
^BC,,Y,N //打印code128的指令
^ FD12345678^FS //设置要打印的内容, ^FD是要打印的条码内容^FS表示换行
^ XZ //条码打印指令结束
*/ //上面的指令会打印12345678的CODE128的条码
/// <summary>
/// 属性类
/// </summary>
public class CreatData
{
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[DllImport("kernel32.dll")]
private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject);
private int iHandle;
public bool Open()
{
iHandle = CreateFile("LPT1:", (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
public bool Write(string Mystring)
{
if (iHandle != -1)
{
int i;
OVERLAPPED x;
byte[] mybyte = Encoding.Default.GetBytes(Mystring);
return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
}
else
{
throw new Exception("LPT1端口未打开!");
}
}
public bool Close()
{
return CloseHandle(iHandle);
}
[DllImport("fnthex32.dll")]
public static extern int GETFONTHEX(
string BarcodeText,
string FontName,
string FileName,
int Orient,
int Height,
int Width,
int IsBold,
int IsItalic,
StringBuilder ReturnBarcodeCMD);
string sBarCodeCMD = ""; //条码打印命令
StringBuilder sb1 = new StringBuilder(10240);
int i1;
//i1 = GETFONTHEX("测试","宋体","temp1",0,30,20,0,0, sb1);
//sBarCodeCMD = sb1.ToString() + "^XA^MD30^LH20,20^FO20,20^XGtemp1,1,1^FS^XZ";
}
}
以上是关于求用java代码控制斑马条码打印机打印二维码商标的例子程序的主要内容,如果未能解决你的问题,请参考以下文章