6

I followed Epson SDK for android to thermal print receipts...

So instead of the text I am given some Image which contains entire image.. at the part of Logoimage(the store).. Which is like below

enter image description here

Here the Store is the Logoimage Insted of that I have given My Recipt(Image) So I am getting like above.. Its not printing full size...

this is my code

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();
        }
    });
}
}

can any one suggest me where do I adjust the size of the image so that I should print the page area instead of logo size...

this is Image content data for Image

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

and this is for image content..

   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);

Please suggest how to print the image content at full size... I am getting a side of page..

It should Print 80 mm of Size but its printing 40 mm can any one suggest me how to make it to full size or how to adjust the size of the Image to stretch it to max paper area...

Whats Going On
  • 1,379
  • 6
  • 20
  • 49

1 Answers1

0

Your question is a bit unclear.

I think your question is:

  • You are given the data for the image at some resolution as an Android BitMapFactory.
  • You want to print on some printer, with another DPI. The Android BitMapFactory for the image is already set.
  • You need to scale the BitMapFactory for your logo.

First, in your line of getting the logo data, you can try just exploring the option BitmapFactory.Options.inSampleSize to get about that right size. That would get you a better, but not perfect answer.

For perfect, you should scale the logo. You can use code here. See also the related questions here and here.

Community
  • 1
  • 1
Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
  • Hi @Charles Merriam Sir, When I print the Data with text.. its printing on the Full paper... Which is left side,,, And in that "the Store" is the Image... So Insted of that Image I have Given Complete Data as Image... But its not printing on Full Page.. Please Help me.. Check my Bitmap Size.. etc.. from my question and suggest me where should I need to change.. – Whats Going On Jul 11 '16 at 09:47