如何在热敏打印机(EPSON)中以完整纸张尺寸(80MM)打印图像

时间:2016-07-01 06:35:50

标签: android printing bitmap thermal-printer

我跟着Epson SDK获取了android的热敏打印收据......

因此,我给了一些包含整个图像的图像而不是文本。 在Logoimage(商店)的一部分..这就像下面

enter image description here

这里的商店是Logoimage Insted,我给了我的Recipt(图片)所以我变得像上面那样..它不是全尺寸打印......

这是我的代码

public class MainActivity extends Activity implements View.OnClickListener, ReceiveListener {

private Context mContext = null;
private EditText mEditTarget = null;
private Spinner mSpnSeries = null;
private Spinner mSpnLang = null;
private Printer  mPrinter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mContext = this;

    int[] target = {
        R.id.btnDiscovery,
        R.id.btnSampleReceipt,
   };

    for (int i = 0; i < target.length; i++) {
        Button button = (Button)findViewById(target[i]);
        button.setOnClickListener(this);
    }

    mSpnSeries = (Spinner)findViewById(R.id.spnModel);
    ArrayAdapter<SpnModelsItem> seriesAdapter = new ArrayAdapter<SpnModelsItem>(this, android.R.layout.simple_spinner_item);
    seriesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    seriesAdapter.add(new SpnModelsItem(getString(R.string.printerseries_t20), Printer.TM_T20));
    mSpnSeries.setAdapter(seriesAdapter);
    mSpnSeries.setSelection(0);

    try {
        Log.setLogSettings(mContext, Log.PERIOD_TEMPORARY, Log.OUTPUT_STORAGE, null, 0, 1, Log.LOGLEVEL_LOW);
    }
    catch (Exception e) {
        ShowMsg.showException(e, "setLogSettings", mContext);
    }
    mEditTarget = (EditText)findViewById(R.id.edtTarget);
}

@Override
protected void onActivityResult(int requestCode, final int resultCode, final Intent data) {
    if (data != null && resultCode == RESULT_OK) {
        String target = data.getStringExtra(getString(R.string.title_target));
        if (target != null) {
            EditText mEdtTarget = (EditText)findViewById(R.id.edtTarget);
            mEdtTarget.setText(target);
        }
    }
}

@Override
public void onClick(View v) {
    Intent intent = null;

    switch (v.getId()) {
        case R.id.btnDiscovery:
            intent = new Intent(this, DiscoveryActivity.class);
            startActivityForResult(intent, 0);
            break;

        case R.id.btnSampleReceipt:
            updateButtonState(false);
            if (!runPrintReceiptSequence()) {
                updateButtonState(true);
            }
            break;

        case R.id.btnSampleCoupon:
            updateButtonState(false);
            if (!runPrintCouponSequence()) {
                updateButtonState(true);
            }
            break;

        default:
            // Do nothing
            break;
    }
}

private boolean runPrintReceiptSequence() {
    if (!initializeObject()) {
        return false;
    }

    if (!createReceiptData()) {
        finalizeObject();
        return false;
    }

    if (!printData()) {
        finalizeObject();
        return false;
    }

    return true;
}

private boolean createReceiptData() {
    String method = "";
    Bitmap logoData = BitmapFactory.decodeResource(getResources(), R.drawable.store);
    StringBuilder textData = new StringBuilder();
    final int barcodeWidth = 2;
    final int barcodeHeight = 100;

    if (mPrinter == null) {
        return false;
    }

    try {
        method = "addTextAlign";
        mPrinter.addTextAlign(Printer.ALIGN_CENTER);

        method = "addImage";
        mPrinter.addImage(logoData, 0, 0,
                          logoData.getWidth(),
                          logoData.getHeight(),
                          Printer.COLOR_1,
                          Printer.MODE_MONO,
                          Printer.HALFTONE_DITHER,
                          Printer.PARAM_DEFAULT,
                          Printer.COMPRESS_AUTO);

        method = "addFeedLine";
        mPrinter.addFeedLine(1);
        textData.append("EPSON PRINT DEMO TEST - (HYD)\n");
        textData.append("STORE DIRECTOR – MLN\n");
        textData.append("\n");
        textData.append("07/06/12 09:15 012 0191 134\n");
        textData.append("ST# 21 OP# 001 TE# 01 TR# 747\n");
        textData.append("------------------------------\n");
        method = "addText";
        mPrinter.addText(textData.toString());
        textData.delete(0, textData.length());

        textData.append("524 3 CUP BLK TEAPOT    9.99 R\n");
        textData.append("003 WESTGATE BLACK 25  59.99 R\n");
        textData.append("------------------------------\n");
        method = "addText";
        mPrinter.addText(textData.toString());
        textData.delete(0, textData.length());

        textData.append("SUBTOTAL                69.98\n");
        textData.append("TAX                      14.43\n");
        method = "addText";
        mPrinter.addText(textData.toString());
        textData.delete(0, textData.length());

        method = "addTextSize";
        mPrinter.addTextSize(2, 2);
        method = "addText";
        mPrinter.addText("TOTAL    84.41\n");
        method = "addTextSize";
        mPrinter.addTextSize(1, 1);
        method = "addFeedLine";
        mPrinter.addFeedLine(1);

        textData.append("CASH                    200.00\n");
        textData.append("CHANGE                   78.14\n");
        textData.append("------------------------------\n");
        method = "addText";
        mPrinter.addText(textData.toString());
        textData.delete(0, textData.length());

        textData.append("Purchased item total number\n");
        textData.append("Sign Up and Save !\n");
        textData.append("With Preferred Saving Card\n");

        method = "addText";
        mPrinter.addText(textData.toString());
        textData.delete(0, textData.length());
        method = "addFeedLine";
        mPrinter.addFeedLine(2);

        method = "addCut";
        mPrinter.addCut(Printer.CUT_FEED);


    }
    catch (Exception e) {
        ShowMsg.showException(e, method, mContext);
        return false;
    }

    textData = null;

    return true;
}

private boolean runPrintCouponSequence() {
    if (!initializeObject()) {
        return false;
    }

    if (!createCouponData()) {
        finalizeObject();
        return false;
    }

    if (!printData()) {
        finalizeObject();
        return false;
    }

    return true;
}


private boolean printData() {
    if (mPrinter == null) {
        return false;
    }

    if (!connectPrinter()) {
        return false;
    }

    PrinterStatusInfo status = mPrinter.getStatus();

    dispPrinterWarnings(status);

    if (!isPrintable(status)) {
        ShowMsg.showMsg(makeErrorMessage(status), mContext);
        try {
            mPrinter.disconnect();
        }
        catch (Exception ex) {
            // Do nothing
        }
        return false;
    }

    try {
        mPrinter.sendData(Printer.PARAM_DEFAULT);
    }
    catch (Exception e) {
        ShowMsg.showException(e, "sendData", mContext);
        try {
            mPrinter.disconnect();
        }
        catch (Exception ex) {
            // Do nothing
        }
        return false;
    }

    return true;
}

private boolean initializeObject() {
    try {
        mPrinter = new Printer(((SpnModelsItem) mSpnSeries.getSelectedItem()).getModelConstant(),
                               ((SpnModelsItem) mSpnLang.getSelectedItem()).getModelConstant(),
                               mContext);
    }
    catch (Exception e) {
        ShowMsg.showException(e, "Printer", mContext);
        return false;
    }

    mPrinter.setReceiveEventListener(this);

    return true;
}

private void finalizeObject() {
    if (mPrinter == null) {
        return;
    }

    mPrinter.clearCommandBuffer();

    mPrinter.setReceiveEventListener(null);

    mPrinter = null;
}

private boolean connectPrinter() {
    boolean isBeginTransaction = false;

    if (mPrinter == null) {
        return false;
    }

    try {
        mPrinter.connect(mEditTarget.getText().toString(), Printer.PARAM_DEFAULT);
    }
    catch (Exception e) {
        ShowMsg.showException(e, "connect", mContext);
        return false;
    }

    try {
        mPrinter.beginTransaction();
        isBeginTransaction = true;
    }
    catch (Exception e) {
        ShowMsg.showException(e, "beginTransaction", mContext);
    }

    if (isBeginTransaction == false) {
        try {
            mPrinter.disconnect();
        }
        catch (Epos2Exception e) {
            // Do nothing
            return false;
        }
    }

    return true;
}

private void disconnectPrinter() {
    if (mPrinter == null) {
        return;
    }

    try {
        mPrinter.endTransaction();
    }
    catch (final Exception e) {
        runOnUiThread(new Runnable() {
            @Override
            public synchronized void run() {
                ShowMsg.showException(e, "endTransaction", mContext);
            }
        });
    }

    try {
        mPrinter.disconnect();
    }
    catch (final Exception e) {
        runOnUiThread(new Runnable() {
            @Override
            public synchronized void run() {
                ShowMsg.showException(e, "disconnect", mContext);
            }
        });
    }

    finalizeObject();
}

private boolean isPrintable(PrinterStatusInfo status) {
    if (status == null) {
        return false;
    }

    if (status.getConnection() == Printer.FALSE) {
        return false;
    }
    else if (status.getOnline() == Printer.FALSE) {
        return false;
    }
    else {
        ;//print available
    }

    return true;
}

private String makeErrorMessage(PrinterStatusInfo status) {
    String msg = "";

    if (status.getBatteryLevel() == Printer.BATTERY_LEVEL_0) {
        msg += getString(R.string.handlingmsg_err_battery_real_end);
    }

    return msg;
}

private void dispPrinterWarnings(PrinterStatusInfo status) {
    EditText edtWarnings = (EditText)findViewById(R.id.edtWarnings);
    String warningsMsg = "";

    if (status == null) {
        return;
    }

    if (status.getPaper() == Printer.PAPER_NEAR_END) {
        warningsMsg += getString(R.string.handlingmsg_warn_receipt_near_end);
    }

    if (status.getBatteryLevel() == Printer.BATTERY_LEVEL_1) {
        warningsMsg += getString(R.string.handlingmsg_warn_battery_near_end);
    }

    edtWarnings.setText(warningsMsg);
}

private void updateButtonState(boolean state) {
    Button btnReceipt = (Button)findViewById(R.id.btnSampleReceipt);
    Button btnCoupon = (Button)findViewById(R.id.btnSampleCoupon);
    btnReceipt.setEnabled(state);
    btnCoupon.setEnabled(state);
}

@Override
public void onPtrReceive(final Printer printerObj, final int code, final PrinterStatusInfo status, final String printJobId) {
    runOnUiThread(new Runnable() {
        @Override
        public synchronized void run() {
            ShowMsg.showResult(code, makeErrorMessage(status), mContext);

            dispPrinterWarnings(status);

            updateButtonState(true);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    disconnectPrinter();
                }
            }).start();
        }
    });
}
}

任何人都可以建议我在哪里调整图像的大小,这样我就可以打印页面区域而不是标识尺寸......

这是图像内容数据 for Image

String method = "";
Bitmap logoData = BitmapFactory.decodeResource(getResources(), R.drawable.store);
//I have given this  Bitmap logoData = BitmapFactory.decodeResource(getResources(), R.drawable.full);

这是图像内容..

   method = "addImage";
   mPrinter.addImage(logoData,0,0,logoData.getWidth(),logoData.getHeight(),Printer.COLOR_1,Printer.MODE_MONO,Printer.HALFTONE_DITHER,Printer.PARAM_DEFAULT,Printer.COMPRESS_AUTO);

请建议如何以全尺寸打印图像内容... 我正在寻找页面的一面..

它应该打印80毫米的尺寸,但它的打印40毫米可以任何人建议我如何使其达到完整尺寸或如何调整图像的大小,以将其拉伸到最大纸张区域......

1 个答案:

答案 0 :(得分:0)

你的问题有点不清楚。

我认为你的问题是:

  • 您可以将某些分辨率的图像数据作为Android BitMapFactory。
  • 您想在某台打印机上打印另一台DPI。已经设置了图像的Android BitMapFactory。
  • 您需要为您的徽标缩放BitMapFactory。

首先,在获取徽标数据的过程中,您可以尝试仅使用BitmapFactory.Options.inSampleSize选项来获得正确的大小。这会让你得到一个更好但不完美的答案。

为了完美,您应该缩放徽标。您可以使用代码Standard deviation。另请参阅相关问题herehere