我花了很長時間才弄清楚如何使其工作。我會盡我所能讓你開始。我對android相當陌生,所以請隨時指出我所做的不正確的事情。它通過將內容向上移動而偶爾會錯誤地進行打印,從而切斷頂部並在底部添加大量空間。如果任何人都可以弄清楚,我會非常感激。
在這裏你去:
需要從APK這些文件:不要相信我修改他們無論如何: 1. RasterDocument.java 2. StarBitmap。的java
主要印刷方法:
public static void PrintReceipt(Context context, RelativeLayout layout){
String portName = "tcp:10.1.250.20"; //ip address of your printer
String portSettings = "";
//have to measure the layout for it to print correctly, otherwise sizes are zero
layout.measure(View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().height, View.MeasureSpec.EXACTLY));
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(),layout.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
layout.draw(canvas);
int maxWidth = 576; //default width of tsp100 receipt
RasterDocument rasterDoc = new RasterDocument(RasterDocument.RasSpeed.Full, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasTopMargin.Standard, 0, 0, 0);
StarBitmap starbitmap = new StarBitmap(bitmap, false, maxWidth);
StarIOPort port = null;
try
{
/*
using StarIOPort3.1.jar (support USB Port)
Android OS Version: upper 2.2
*/
port = StarIOPort.getPort(portName, portSettings, 10000, context);
/*
using StarIOPort.jar
Android OS Version: under 2.1
port = StarIOPort.getPort(portName, portSettings, 10000);
*/
try
{
Thread.sleep(500);
}
catch(InterruptedException e) {}
byte[] command = rasterDoc.BeginDocumentCommandData();
port.writePort(command, 0, command.length);
command = starbitmap.getImageRasterDataForPrinting();
port.writePort(command, 0, command.length);
command = rasterDoc.EndDocumentCommandData();
port.writePort(command, 0, command.length);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e) {}
}
catch (StarIOPortException e)
{
ShowAlertMessage(context, "Failure", "Failed to connect to printer. " + e.getMessage());
}
finally
{
if(port != null)
{
try {
StarIOPort.releasePort(port);
} catch (StarIOPortException e) {}
}
}
}
private static void ShowAlertMessage(final Context context, final String alertTitle, final String message){
try {
((Activity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setNegativeButton("Ok", null);
AlertDialog alert = dialog.create();
alert.setTitle(alertTitle);
alert.setMessage(message);
alert.show();
}});
} catch (final Exception e) {
Log.e(PrinterFunctions.class.getName(), e.getMessage());
}
}
THEN: 在另一方法中,發送一個PrintReceipt RelativeLayout的。在類的構造函數中設置當前上下文。
Context currentContext;
RelativeLayout relativeLayout;
private final int receiptWidth = 576;
private Typeface typeFace = Typeface.DEFAULT;
private int normalFontSize = 23;
private int largeFontSize = 28;
public SetupReceiptClass(Context context){
currentContext = context;
}
public void SetupReceipt(String customerName){
//Create layout for receipt
relativeLayout = new RelativeLayout(currentContext);
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(receiptWidth, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(params);
relativeLayout.setId(R.id.ReceiptLayout);
//Create whatever views you want here and add them to the RelativeLayout to make up your receipt
relativeLayout.addView(whateverViewsYouCreate);
//Finally, Print the receipt.
new Thread(new Runnable() {
@Override
public void run() {
PrintReceipt(currentContext, relativeLayout);
}
}).start();
}
再一次,我是新來的android的東西,這可能不是最好的方式,但它是打印。任何酷你發現我很想聽到他們。
有沒有人能夠解決這個問題?我有同樣的問題.thx – 2011-10-15 04:38:02
你有沒有找到你的查詢解決方案?有沒有辦法解決這個問題。 –