Wie kann ich ein Bild auf einem Bluetooth-Drucker in Android drucken?

Ich muss einige Daten auf einem Bluetooth-Thermodrucker drucken. Dabei mache ich Folgendes:

String message="abcdef any message 12345";
byte[] send;
send = message.getBytes();
mService.write(send);

Es funktioniert gut für Text, aber nicht für Bilder. Ich denke, ich muss das bekommenbyte[] der Bilddaten. Ich habe versucht, die Daten des Bildes auf folgende Weise abzurufen:

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.qrcode);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();

Leider druckt der Drucker viele seltsame Zeichen (ca. 50 cm Papier). Ich weiß nicht, wie ich das Bild ausdrucken soll.

Ich würde gerne versuchen, die Pixel der Bitmap zu bekommen und sie als nächstes in eine umzuwandelnbyte[] und schicke es, aber ich weiß nicht, wie es geht.

Vielen Dank

AKTUALISIEREN:

Nach so viel Zeit mache ich Folgendes: Ich habe eine Methode namens print_image (String-Datei), die den Pfad des Bildes abruft, das ich drucken möchte:

private void print_image(String file) {
    File fl = new File(file);
    if (fl.exists()) {
        Bitmap bmp = BitmapFactory.decodeFile(file);
        convertBitmap(bmp);
        mService.write(PrinterCommands.SET_LINE_SPACING_24);

        int offset = 0;
        while (offset < bmp.getHeight()) {
            mService.write(PrinterCommands.SELECT_BIT_IMAGE_MODE);
            for (int x = 0; x < bmp.getWidth(); ++x) {

                for (int k = 0; k < 3; ++k) {

                    byte slice = 0;
                    for (int b = 0; b < 8; ++b) {
                        int y = (((offset / 8) + k) * 8) + b;
                        int i = (y * bmp.getWidth()) + x;
                        boolean v = false;
                        if (i < dots.length()) {
                            v = dots.get(i);
                        }
                        slice |= (byte) ((v ? 1 : 0) << (7 - b));
                    }
                    mService.write(slice);
                }
            }
            offset += 24;
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);          
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
        }
        mService.write(PrinterCommands.SET_LINE_SPACING_30);


    } else {
        Toast.makeText(this, "file doesn't exists", Toast.LENGTH_SHORT)
                .show();
    }
}

Ich habe es basierend darauf gemachtPost

Dies ist die Klasse PrinterCommands:

public class PrinterCommands {
public static final byte[] INIT = {27, 64};
public static byte[] FEED_LINE = {10};

public static byte[] SELECT_FONT_A = {27, 33, 0};

public static byte[] SET_BAR_CODE_HEIGHT = {29, 104, 100};
public static byte[] PRINT_BAR_CODE_1 = {29, 107, 2};
public static byte[] SEND_NULL_BYTE = {0x00};

public static byte[] SELECT_PRINT_SHEET = {0x1B, 0x63, 0x30, 0x02};
public static byte[] FEED_PAPER_AND_CUT = {0x1D, 0x56, 66, 0x00};

public static byte[] SELECT_CYRILLIC_CHARACTER_CODE_TABLE = {0x1B, 0x74, 0x11};

public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, -128, 0};
public static byte[] SET_LINE_SPACING_24 = {0x1B, 0x33, 24};
public static byte[] SET_LINE_SPACING_30 = {0x1B, 0x33, 30};

public static byte[] TRANSMIT_DLE_PRINTER_STATUS = {0x10, 0x04, 0x01};
public static byte[] TRANSMIT_DLE_OFFLINE_PRINTER_STATUS = {0x10, 0x04, 0x02};
public static byte[] TRANSMIT_DLE_ERROR_STATUS = {0x10, 0x04, 0x03};
public static byte[] TRANSMIT_DLE_ROLL_PAPER_SENSOR_STATUS = {0x10, 0x04, 0x04};
}

Wie in der print_image-Methode zu sehen ist, rufe ich eine Methode namens convertBitmap auf und sende eine Bitmap. Dies ist der Code:

   public String convertBitmap(Bitmap inputBitmap) {

    mWidth = inputBitmap.getWidth();
    mHeight = inputBitmap.getHeight();

    convertArgbToGrayscale(inputBitmap, mWidth, mHeight);
    mStatus = "ok";
    return mStatus;

}

private void convertArgbToGrayscale(Bitmap bmpOriginal, int width,
        int height) {
    int pixel;
    int k = 0;
    int B = 0, G = 0, R = 0;
    dots = new BitSet();
    try {

        for (int x = 0; x < height; x++) {
            for (int y = 0; y < width; y++) {
                // get one pixel color
                pixel = bmpOriginal.getPixel(y, x);

                // retrieve color of all channels
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value by calculating
                // pixel intensity.
                R = G = B = (int) (0.299 * R + 0.587 * G + 0.114 * B);
                // set bit into bitset, by calculating the pixel's luma
                if (R < 55) {                       
                    dots.set(k);//this is the bitset that i'm printing
                }
                k++;

            }


        }


    } catch (Exception e) {
        // TODO: handle exception
        Log.e(TAG, e.toString());
    }
}

Dies ist dasDrucker die ich benutze, Auflösung: 8 Punkte / mm, 576 Punkte / Linie

Und das ist, was ich gerne mache (ich habe es mit demselben Drucker gemacht, aber mit einer App, die aus dem Play Store heruntergeladen wurde)

Das ist es, was ich jetzt bekomme

Näher:

Closer2:

Ein kleiner Teil des Bildes ist zu sehen, daher denke ich, dass ich näher dran bin, um das Bild zu drucken ...

Das Bild, das ich benutze, ist dieses (576x95):

Und dies ist das konvertierte Bild (ich konvertiere es mit dem oberen Code):

Die Antwort lautet also: Was mache ich falsch? Ich denke, dass der Fehler in diesem Befehl liegt:

  public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, -128, 0};

Aber wie kann ich die korrekten Werte für mein Bild berechnen ?, danke

Antworten auf die Frage(7)

Ihre Antwort auf die Frage