anastaciocintra / escpos-coffee

Java library for ESC/POS printer
https://anastaciocintra.github.io/escpos-coffee
MIT License
280 stars 74 forks source link

Woking Fine But some time image not printing printing bad character in android #42

Open lavahasif opened 4 years ago

lavahasif commented 4 years ago

print 20 print continuos no problem but some time original some time showing this error anyway thank you for solution helped

code: public void run(String ip, Bitmap bmp, PrintBillFormat printBillFormat) { String host = ip; int port = 9100; try (TcpIpOutputStream stream = new TcpIpOutputStream(host, port)) { EscPos escpos = new EscPos(stream);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        Bitmap scaledBitmap = bmp;
        double heightformat = 0.0;
        int widthformat = 0;

        try {
            heightformat = Double.parseDouble(printBillFormat.getTheight());
            widthformat = Integer.parseInt(printBillFormat.getTwidth());
        } catch (NumberFormatException e) {
            heightformat = 1.2;
            widthformat = 500;
            e.printStackTrace();
        }
        int IntValue = (int) (scaledBitmap.getHeight() * heightformat);
        Log.i("value", Integer.toString(IntValue));
        Bitmap bitmap;
        if (printBillFormat.getScale().equalsIgnoreCase("off")) {
            bitmap = scaledBitmap;

        } else {
            bitmap = Bitmap.createScaledBitmap(scaledBitmap, widthformat, IntValue, true);
        }

        RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();

        Bitonal algorithm = new BitonalOrderedDither();
        EscPosImage escposImage = new EscPosImage(new CoffeeImageAndroidImpl(bitmap), algorithm);

        escpos.write(imageWrapper, escposImage);
        if (printBillFormat != null)
            escpos.feed(printBillFormat.getFeedpaper()).cut(EscPos.CutMode.FULL);
        else
            escpos.feed(1).cut(EscPos.CutMode.FULL);

        if (bmp != null) {
            bitmap.recycle();
        }

        escposImage = null;
        bitmap = null;
    } catch (IOException e) {

        Toast.makeText(context, "Check Your ip or Printer not listen", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

    if (bmp != null) {
        bmp.recycle();
    }
    bmp = null;
}

can you suggest to preview escposImage as image file

anastaciocintra commented 4 years ago

Hi @lavahasif I'm misconfused about "can you suggest to preview escposImage as image file" Are you asking about I have one UI viewer of escpos-image dump? or are you asking if it can send the dump to one file?

However, in my point of view, first we need to isolate the problem. The problem can be simulated? If yes, I can help you to debug and find the problem.

if not: In my analisis, the problem can happen on some groups, and you can dump file results the variables: bitmap imageWrapper TcpIpOutputStream

        RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();

        Bitonal algorithm = new BitonalOrderedDither();
        EscPosImage escposImage = new EscPosImage(new CoffeeImageAndroidImpl(bitmap), algorithm);

        escpos.write(imageWrapper, escposImage);

        // save imageWrapper to one file to print...
        byte[] bytes = imageWrapper.getBytes(escposImage);
        // create one file and save it...

        // save the bitmap to another file (png)

them, when the problem happen, Analising the files, you can conclude where the problem is.

count on me, Marco

lavahasif commented 4 years ago

Thank you for your quick replay.

### {I'm misconfused about "can you suggest to preview escposImage as image file" Are you asking about I have one UI viewer of escpos-image dump? or are you asking if it can send the dump to one file?}

yes I want to save the esposimage variable as a image file for checking(I have one UI viewer of escpos-image dump) .some time the pritnting not work properly (https://user-images.githubusercontent.com/22430922/90307980-2bb92b80-def9-11ea-8823-2a6388d3a844.jpeg ) not always . so i want to check where it happen.

anastaciocintra commented 4 years ago

I made one variant of EscPos Class, you can use it to dump file content on android

You can use traditional write that only dump the escpos_image, or one more complete function version writeAndDumpFiles that dump the escpos image and dump the Bitmap too..

I hope this help you. till later.

package com.github.anastaciocintra.escposcoffeesamples.androidimage;

import android.content.Context;
import android.graphics.Bitmap;

import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.escpos.image.Bitonal;
import com.github.anastaciocintra.escpos.image.EscPosImage;
import com.github.anastaciocintra.escpos.image.ImageWrapperInterface;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EscposWithDumpAndroid extends EscPos {
    private Context context;

    /**
     * creates an instance based on outputStream.
     *
     * @param outputStream can be one file, System.out or printer...
     * @see OutputStream
     */
    public EscposWithDumpAndroid(OutputStream outputStream, Context context) {
        super(outputStream);
        this.context = context;
    }

    @Override
    public EscPos write(ImageWrapperInterface wrapper, EscPosImage image) throws IOException {
        // call original write
        super.write(wrapper, image);

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = dateFormat.format(new Date());
        String fileName = strDate + ".dump";
        // creating file...
        // https://developer.android.com/training/data-storage/app-specific
        FileOutputStream fileDump = context.openFileOutput(fileName, Context.MODE_PRIVATE);

        // dump escpos_image
        byte[] bytes = wrapper.getBytes(image);
        fileDump.write(bytes);

        // close file
        fileDump.close();

        return this;
    }
    // other and more complete option
    // dump both files, the EscposImage and the bitmap
    public EscPos writeAndDumpFiles(ImageWrapperInterface wrapper, Bitonal algorithm, Bitmap bitmap) throws IOException {

        EscPosImage escposImage = new EscPosImage(new CoffeeImageAndroidImpl(bitmap), algorithm);

        super.write(wrapper, escposImage);

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = dateFormat.format(new Date());
        String fileName = strDate + ".dump";
        // creating file...
        // https://developer.android.com/training/data-storage/app-specific
        FileOutputStream fileDump = context.openFileOutput(fileName, Context.MODE_PRIVATE);

        // dump escpos_image
        byte[] bytes = wrapper.getBytes(escposImage);
        fileDump.write(bytes);

        // close file
        fileDump.close();

        // Bitmap
        fileName = strDate + ".png";
        fileDump = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileDump);
        fileDump.close();
        return this;
    }

}
anastaciocintra commented 4 years ago

Hi @lavahasif recently, i discovered that if the image have width or height bigger than printer supported, then can happen this, one overflow, and the printer output will be unpredictable.

You can log the image dimensions to diagnose if is this happening.

anastaciocintra commented 4 years ago

read this

lavahasif commented 4 years ago

Hi @lavahasif recently, i discovered that if the image have width or height bigger than printer supported, then can happen this, one overflow, and the printer output will be unpredictable.

You can log the image dimensions to diagnose if is this happening.

Thank you for your effort.Some time i feel it.i already mention above .For checking i print 20 page simultaneously .i did'nt get any garbage .some time get above character.my customer have daily 300 bills.some time they get garbage print.so i change the above model now i am using printer font with logo now no problem.some time i feel may be my app have memory leak(if i get garbage then i try to restart the printer then take bill get correctly). Due to that reason some time getting that print.Thanks Could you suggest any method accessing shared printer from windows in android.(now i am using webapi selfhosted service to maintain the problem)

lavahasif commented 4 years ago

Hi @lavahasif recently, i discovered that if the image have width or height bigger than printer supported, then can happen this, one overflow, and the printer output will be unpredictable.

You can log the image dimensions to diagnose if is this happening.

My printing Method was :-

Collect Printing Data --->Convert to html (width 300px 'variation get big or overflow print(i didnt get any garbage )'-->html to image(Here i try to save 60 or above continous save bill to internal storage no problem )-->escpos

robincursor commented 1 year ago

hi, i have the same problem, in my case it only works for the first time after i turn on the printer, and then only print random charatcters. My solution is this: after i create escpos object then i call escPos.initializePrinter(); and after a the full printing proces i call it once again before close: escPos.flush();escPos.initializePrinter();escPos.close(); And it wokrs for me, but its not perfect because the justification is not ok... And of course it depends on the printer eg on the sam4s printer works fine without any tricks, and a on chinise noname printer i need this tricks above