nanto
nanto

Reputation: 13

Printing PNG logo on TSC DA220 printer in Java Android

I am developing an Android app with Java in Android Studio, aiming to print labels on a TSC DA220 printer using TSPL commands. My goal is to include the company logo on the labels, which is stored as a .png file in the res/drawables directory.

I first attempted to print the image using the BITMAP X, Y, width, height, mode, bitmap data command by converting the logo to a String representing the array of bytes. Here is the code snippet I used for this approach:

public class ChosenAndCookedSticker implements PrintableModel, TsplLanguage.Support {

    ...
    
    @Override
    public List<Command> getTsplCommandList() {
        List<Command> commandList = new ArrayList<>();
    
        commandList.add(new DirectionCommandTspl(DirectionCommandTspl.REVERSE));
    
        commandList.add(new LabelSizeCommandTspl(new DimensionTspl(58, Dimension.MM), new DimensionTspl(58, Dimension.MM)));
    
        commandList.add(new PrintSpeedCommandTspl(PrintSpeedCommandTspl.SPEED_4_INCH_PER_SECOND));
    
        commandList.add(new DensityCommandTspl(15));
    
        commandList.add(new FeedCommandTspl(FeedCommandTspl.GAP_FEED, new DimensionTspl(3, Dimension.MM), new DimensionTspl(0, Dimension.MM)));
    
        commandList.add(new ImageBufferClearCommandTspl());
    
        commandList.add(new PrintQualityCommandTspl(PrintQualityCommandTspl.OPTIMUM));
    
        commandList.add(new CodepageTspl(CodepageTspl.UTF8));
    
        String hexImage = convertImageToHex(R.drawable.logo);
        commandList.add(new BitmapCommandTspl(
            new DimensionTspl(25, Dimension.MM),
            new DimensionTspl(1.2, Dimension.MM),
            new DimensionTspl(10, Dimension.MM),
            new DimensionTspl(5, Dimension.MM),
            0,
            hexImage));
            
        return commandList;
    }
    
    private String convertImageToHex(int drawableId) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId);
        byte[] bytes = convertBitmapToByteArray(bitmap);
        
        return convertByteArrayToHex(bytes);
    }
    
    private String convertByteArrayToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        
        return hexString.toString();
    }
    
    private byte[] convertBitmapToByteArray(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }
    
    ...

}
public class BitmapCommandTspl extends Command {
    private final DimensionTspl x;
    private final DimensionTspl y;
    private final DimensionTspl width;
    private final DimensionTspl height;
    private final int mode;
    private final String bitmapData;

    public BitmapCommandTspl(DimensionTspl x, DimensionTspl y, DimensionTspl width, DimensionTspl height, int mode, String bitmapData) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.mode = mode;
        this.bitmapData = bitmapData;
    }

    @Override
    public String getCommand() {
        return String.format(
                "BITMAP %d,%d,%d,%d,%d,\"%s\"",
                x, y, width, height, mode, bitmapData
        );
    }
}

I expected the printer to output the logo, however, instead of the expected image, the printer produced a black strip with the correct dimensions but devoid of any actual image content.

Considering a different approach, I then looked into the PUTPNG command, which is designed for handling PNG files. The command format I found in the documentation is PUTPNG x, y, "filename". My challenge here is figuring out how to reference the drawable PNG file correctly within this TSPL command, as simply specifying the file path that works within Android does not seem to translate directly to printer-readable commands.

Upvotes: 0

Views: 321

Answers (1)

ECB-RS-BR
ECB-RS-BR

Reputation: 1

The way I do it is as follows. Sending images in BMP to the printer using the TSC Console. Then just use the command:

PUTBMP 89,10,"YourImageName.BMP",8,100

I use the command as a parameter within other commands,
respecting that the command to the printer,
must be delivered at formatted in its view, after all previous processing.
If you want to deliver variables in some of the parameters,
you need to close, concatenate your variable and reopen the string
with the rest of parameters in TSPL command, until it ends up.
At least that's the way it works when I use "sendcommand(command)" from TSCLIB.DLL.

From TSC.sendcommand("PUTBMP "_X_","_Y_",""IMAGE.BMP"",8,100")

In the example above, notice that my variables X and Y are outside the string.
And they were concatenated with the string that is delivering the command through the _ (underscore).
The file name still has a particularity as it needs to be a string,
within the third parameter of the PUTBMP command.
So this parameter does not need to close the string,
but it needs to receive a signal that generates double quotes at the beginning
and end of the name of the file that you will access from memory.
In my case, I provide two double quotes together at the beginning and at the end "",
to be delivered as a string within the third parameter of the PUTBMP command.

Upvotes: 0

Related Questions