mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
Printer: allow alignment of picture
Show the picture on a canvas and allow to swipe it to align left, middle or right. Also simplify the activity layout by removing the incoming picture and only showing the preview.
This commit is contained in:
+35
-7
@@ -6,6 +6,8 @@ import android.graphics.Bitmap;
|
|||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.GestureDetector;
|
||||||
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
@@ -40,15 +42,39 @@ import nodomain.freeyourgadget.gadgetbridge.util.UriHelper;
|
|||||||
public class SendToPrinterActivity extends AbstractGBActivity {
|
public class SendToPrinterActivity extends AbstractGBActivity {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(SendToPrinterActivity.class);
|
private static final Logger LOG = LoggerFactory.getLogger(SendToPrinterActivity.class);
|
||||||
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
|
GenericThermalPrinterSupport.PrintAlignment alignment = GenericThermalPrinterSupport.PrintAlignment.ALIGN_CENTER;
|
||||||
private Bitmap bitmap;
|
private Bitmap bitmap;
|
||||||
private Bitmap incoming;
|
private Bitmap incoming;
|
||||||
private ImageView previewImage;
|
private ImageView previewImage;
|
||||||
private ImageView incomingImage;
|
|
||||||
private MaterialSwitch dithering;
|
private MaterialSwitch dithering;
|
||||||
private MaterialSwitch upscale;
|
private MaterialSwitch upscale;
|
||||||
private BitmapToBitSet bitmapToBitSet;
|
private final GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
|
||||||
private File printPicture = null;
|
private static final int SWIPE_THRESHOLD = 100;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onDown(MotionEvent e) {
|
||||||
|
return true; // needed to enable fling detection
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
||||||
|
float diffX = e2.getX() - e1.getX();
|
||||||
|
|
||||||
|
if (Math.abs(diffX) > Math.abs(e2.getY() - e1.getY())) {
|
||||||
|
if (Math.abs(diffX) > SWIPE_THRESHOLD) {
|
||||||
|
if (diffX > 0) {
|
||||||
|
alignment = alignment == GenericThermalPrinterSupport.PrintAlignment.ALIGN_LEFT ? GenericThermalPrinterSupport.PrintAlignment.ALIGN_CENTER : GenericThermalPrinterSupport.PrintAlignment.ALIGN_RIGHT;
|
||||||
|
} else {
|
||||||
|
alignment = alignment == GenericThermalPrinterSupport.PrintAlignment.ALIGN_RIGHT ? GenericThermalPrinterSupport.PrintAlignment.ALIGN_CENTER : GenericThermalPrinterSupport.PrintAlignment.ALIGN_LEFT;
|
||||||
|
}
|
||||||
|
updatePreview();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private File printPicture = null;
|
||||||
ActivityResultLauncher<Intent> pickImageLauncher = registerForActivityResult(
|
ActivityResultLauncher<Intent> pickImageLauncher = registerForActivityResult(
|
||||||
new ActivityResultContracts.StartActivityForResult(),
|
new ActivityResultContracts.StartActivityForResult(),
|
||||||
result -> {
|
result -> {
|
||||||
@@ -58,13 +84,13 @@ public class SendToPrinterActivity extends AbstractGBActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
private GestureDetector gestureDetector;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
setContentView(R.layout.activity_print_image);
|
setContentView(R.layout.activity_print_image);
|
||||||
incomingImage = findViewById(R.id.incomingImage);
|
|
||||||
previewImage = findViewById(R.id.convertedImage);
|
previewImage = findViewById(R.id.convertedImage);
|
||||||
Button sendToPrinter = findViewById(R.id.sendToPrinterButton);
|
Button sendToPrinter = findViewById(R.id.sendToPrinterButton);
|
||||||
dithering = findViewById(R.id.switchDithering);
|
dithering = findViewById(R.id.switchDithering);
|
||||||
@@ -88,6 +114,9 @@ public class SendToPrinterActivity extends AbstractGBActivity {
|
|||||||
sendToPrinter.setEnabled(false);
|
sendToPrinter.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gestureDetector = new GestureDetector(this, gestureListener);
|
||||||
|
|
||||||
|
previewImage.setOnTouchListener((v, event) -> gestureDetector.onTouchEvent(event));
|
||||||
dithering.setOnClickListener(new View.OnClickListener() {
|
dithering.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
@@ -169,9 +198,7 @@ public class SendToPrinterActivity extends AbstractGBActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updatePreview() {
|
private void updatePreview() {
|
||||||
incomingImage.setImageBitmap(bitmap);
|
final BitmapToBitSet bitmapToBitSet = new BitmapToBitSet(GenericThermalPrinterSupport.createAlignedBitmap(bitmap, alignment));
|
||||||
|
|
||||||
bitmapToBitSet = new BitmapToBitSet(bitmap);
|
|
||||||
bitmapToBitSet.toBlackAndWhite(dithering.isChecked());
|
bitmapToBitSet.toBlackAndWhite(dithering.isChecked());
|
||||||
|
|
||||||
TextView imageSizeText = findViewById(R.id.imageSizeText);
|
TextView imageSizeText = findViewById(R.id.imageSizeText);
|
||||||
@@ -185,6 +212,7 @@ public class SendToPrinterActivity extends AbstractGBActivity {
|
|||||||
intent.putExtra(GenericThermalPrinterSupport.INTENT_EXTRA_BITMAP_CACHE_FILE_PATH, printPicture.getAbsolutePath());
|
intent.putExtra(GenericThermalPrinterSupport.INTENT_EXTRA_BITMAP_CACHE_FILE_PATH, printPicture.getAbsolutePath());
|
||||||
intent.putExtra(GenericThermalPrinterSupport.INTENT_EXTRA_APPLY_DITHERING, dithering.isChecked());
|
intent.putExtra(GenericThermalPrinterSupport.INTENT_EXTRA_APPLY_DITHERING, dithering.isChecked());
|
||||||
intent.putExtra(GenericThermalPrinterSupport.INTENT_EXTRA_UPSCALE, upscale.isChecked());
|
intent.putExtra(GenericThermalPrinterSupport.INTENT_EXTRA_UPSCALE, upscale.isChecked());
|
||||||
|
intent.putExtra(GenericThermalPrinterSupport.INTENT_EXTRA_ALIGNMENT, alignment);
|
||||||
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+87
-76
@@ -43,15 +43,15 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
|
||||||
public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSupport {
|
public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSupport {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(GenericThermalPrinterSupport.class);
|
|
||||||
public static final int IMAGE_WIDTH = 384;
|
public static final int IMAGE_WIDTH = 384;
|
||||||
public static final String INTENT_ACTION_PRINT_BITMAP = "print_bitmap";
|
public static final String INTENT_ACTION_PRINT_BITMAP = "print_bitmap";
|
||||||
public static final String INTENT_EXTRA_URI = "picture_uri";
|
public static final String INTENT_EXTRA_URI = "picture_uri";
|
||||||
public static final String INTENT_EXTRA_BITMAP_CACHE_FILE_PATH = "bitmap_cache_file_path";
|
public static final String INTENT_EXTRA_BITMAP_CACHE_FILE_PATH = "bitmap_cache_file_path";
|
||||||
public static final String INTENT_EXTRA_APPLY_DITHERING = "apply_dithering";
|
public static final String INTENT_EXTRA_APPLY_DITHERING = "apply_dithering";
|
||||||
public static final String INTENT_EXTRA_UPSCALE = "upscale";
|
public static final String INTENT_EXTRA_UPSCALE = "upscale";
|
||||||
|
public static final String INTENT_EXTRA_ALIGNMENT = "alignment";
|
||||||
public static final UUID discoveryService = UUID.fromString("0000af30-0000-1000-8000-00805f9b34fb");
|
public static final UUID discoveryService = UUID.fromString("0000af30-0000-1000-8000-00805f9b34fb");
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(GenericThermalPrinterSupport.class);
|
||||||
private final UUID writeCharUUID = UUID.fromString("0000ae01-0000-1000-8000-00805f9b34fb");
|
private final UUID writeCharUUID = UUID.fromString("0000ae01-0000-1000-8000-00805f9b34fb");
|
||||||
private final UUID notifCharUUID = UUID.fromString("0000ae02-0000-1000-8000-00805f9b34fb");
|
private final UUID notifCharUUID = UUID.fromString("0000ae02-0000-1000-8000-00805f9b34fb");
|
||||||
private final Handler handler = new Handler(Looper.getMainLooper());
|
private final Handler handler = new Handler(Looper.getMainLooper());
|
||||||
@@ -59,13 +59,11 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
LOG.info("Running retrieving battery through runner.");
|
LOG.info("Running retrieving battery through runner.");
|
||||||
send("getDevState", PrinterCommand.getDevState.message(new byte[]{0x00}));
|
send("getDevState", PrinterCommand.getDevState.message(new byte[]{0x00}));
|
||||||
};
|
};
|
||||||
private boolean useRunLengthEncoding = false;
|
|
||||||
private boolean canPrint = false;
|
|
||||||
|
|
||||||
private final int PRINT_INTENSITY = 8000;
|
private final int PRINT_INTENSITY = 8000;
|
||||||
private final int PRINT_SPEED = 10;
|
private final int PRINT_SPEED = 10;
|
||||||
private final int PRINT_TYPE = 0; //1 also observed
|
private final int PRINT_TYPE = 0; //1 also observed
|
||||||
|
private boolean useRunLengthEncoding = false;
|
||||||
|
private boolean canPrint = false;
|
||||||
private final BroadcastReceiver printCommandReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver printCommandReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
@@ -87,7 +85,8 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
boolean dither = intent.getBooleanExtra(INTENT_EXTRA_APPLY_DITHERING, false);
|
boolean dither = intent.getBooleanExtra(INTENT_EXTRA_APPLY_DITHERING, false);
|
||||||
printImage(bitmap, dither);
|
PrintAlignment alignment = (PrintAlignment) intent.getSerializableExtra(INTENT_EXTRA_ALIGNMENT);
|
||||||
|
printImage(bitmap, dither, alignment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -98,19 +97,7 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] byteBufferToArray(final ByteBuffer input) {
|
private static Bitmap createBitmapFromString(String text) {
|
||||||
input.flip();
|
|
||||||
final byte[] result = new byte[input.limit()];
|
|
||||||
input.get(result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNotification(NotificationSpec notificationSpec) {
|
|
||||||
// printImage(createNotificationBitmap(notificationSpec), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Bitmap createNotificationBitmap(NotificationSpec spec) {
|
|
||||||
float textSize = 24f; // Approx 10pt at 200 dpi
|
float textSize = 24f; // Approx 10pt at 200 dpi
|
||||||
|
|
||||||
// Prepare the paint for drawing text
|
// Prepare the paint for drawing text
|
||||||
@@ -119,22 +106,6 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
textPaint.setTextSize(textSize);
|
textPaint.setTextSize(textSize);
|
||||||
textPaint.setTypeface(Typeface.DEFAULT);
|
textPaint.setTypeface(Typeface.DEFAULT);
|
||||||
|
|
||||||
StringBuilder textBuilder = new StringBuilder();
|
|
||||||
if (spec.sender != null) {
|
|
||||||
textBuilder.append("Sender: ").append(spec.sender).append("\n");
|
|
||||||
}
|
|
||||||
if (spec.title != null) {
|
|
||||||
textBuilder.append("Title: ").append(spec.title).append("\n");
|
|
||||||
}
|
|
||||||
if (spec.subject != null) {
|
|
||||||
textBuilder.append("Subject: ").append(spec.subject).append("\n\n");
|
|
||||||
}
|
|
||||||
if (spec.body != null) {
|
|
||||||
textBuilder.append(spec.body);
|
|
||||||
}
|
|
||||||
|
|
||||||
String text = textBuilder.toString();
|
|
||||||
|
|
||||||
// Create StaticLayout to handle text wrapping
|
// Create StaticLayout to handle text wrapping
|
||||||
StaticLayout staticLayout = new StaticLayout(text, textPaint, IMAGE_WIDTH,
|
StaticLayout staticLayout = new StaticLayout(text, textPaint, IMAGE_WIDTH,
|
||||||
Layout.Alignment.ALIGN_NORMAL,
|
Layout.Alignment.ALIGN_NORMAL,
|
||||||
@@ -144,7 +115,7 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
int bitmapHeight = staticLayout.getHeight();
|
int bitmapHeight = staticLayout.getHeight();
|
||||||
|
|
||||||
// Create the bitmap
|
// Create the bitmap
|
||||||
Bitmap bitmap = Bitmap.createBitmap(IMAGE_WIDTH, bitmapHeight, Bitmap.Config.ARGB_8888);
|
final Bitmap bitmap = Bitmap.createBitmap(IMAGE_WIDTH, bitmapHeight, Bitmap.Config.ARGB_8888);
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
|
|
||||||
// Draw the text onto the canvas using the StaticLayout
|
// Draw the text onto the canvas using the StaticLayout
|
||||||
@@ -153,6 +124,13 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static byte[] byteBufferToArray(final ByteBuffer input) {
|
||||||
|
input.flip();
|
||||||
|
final byte[] result = new byte[input.limit()];
|
||||||
|
input.get(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private static int getCrc8(final byte[] seq) {
|
private static int getCrc8(final byte[] seq) {
|
||||||
int crc = 0x00;
|
int crc = 0x00;
|
||||||
|
|
||||||
@@ -171,6 +149,53 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
return (byte) (crc & 0xFF);
|
return (byte) (crc & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNotification(NotificationSpec notificationSpec) {
|
||||||
|
// printImage(createNotificationBitmap(notificationSpec), true, PrintAlignment.ALIGN_LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bitmap createNotificationBitmap(NotificationSpec spec) {
|
||||||
|
|
||||||
|
StringBuilder textBuilder = new StringBuilder();
|
||||||
|
if (spec.sender != null) {
|
||||||
|
textBuilder.append("Sender: ").append(spec.sender).append("\n");
|
||||||
|
}
|
||||||
|
if (spec.title != null) {
|
||||||
|
textBuilder.append("Title: ").append(spec.title).append("\n");
|
||||||
|
}
|
||||||
|
if (spec.subject != null) {
|
||||||
|
textBuilder.append("Subject: ").append(spec.subject).append("\n\n");
|
||||||
|
}
|
||||||
|
if (spec.body != null) {
|
||||||
|
textBuilder.append(spec.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
return createBitmapFromString(textBuilder.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printImage(Bitmap bitmap, boolean applyDithering, PrintAlignment alignment) {
|
||||||
|
if (!canPrint) {
|
||||||
|
LOG.error("Printer cannot print.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final BitmapToBitSet bitmapToBitSet = new BitmapToBitSet(createAlignedBitmap(bitmap, alignment));
|
||||||
|
|
||||||
|
final ByteBuffer result = ByteBuffer.allocate(500 + (int) (Math.ceil(bitmap.getWidth() / 8) + 8) * bitmap.getHeight()); //TODO: approximate better?
|
||||||
|
|
||||||
|
result.put(PrinterCommand.intensity(PRINT_INTENSITY));
|
||||||
|
result.put(PrinterCommand.printSpeed.message(new byte[]{PRINT_SPEED}));
|
||||||
|
result.put(PrinterCommand.printType.message(new byte[]{PRINT_TYPE}));
|
||||||
|
result.put(PrinterCommand.quality(5));
|
||||||
|
|
||||||
|
result.put(encodePictureToPrinterCommands(bitmapToBitSet.toBlackAndWhite(applyDithering), bitmapToBitSet.getWidth(), bitmapToBitSet.getHeight()));
|
||||||
|
result.put(PrinterCommand.feedPaper(72));
|
||||||
|
|
||||||
|
result.put(PrinterCommand.getDevState.message(new byte[]{0})); //to get back the current device status
|
||||||
|
|
||||||
|
send("Print...", byteBufferToArray(result));
|
||||||
|
}
|
||||||
|
|
||||||
private byte[] encodePictureToPrinterCommands(BitSet imageBits, int imageWidth, int imageHeight) {
|
private byte[] encodePictureToPrinterCommands(BitSet imageBits, int imageWidth, int imageHeight) {
|
||||||
final int maxOctetsPerRow = (imageWidth + 7) / 8; //always round up
|
final int maxOctetsPerRow = (imageWidth + 7) / 8; //always round up
|
||||||
final ByteBuffer result = ByteBuffer.allocate(imageBits.length() + 1000);
|
final ByteBuffer result = ByteBuffer.allocate(imageBits.length() + 1000);
|
||||||
@@ -204,26 +229,27 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printImage(Bitmap bitmap, boolean applyDithering) {
|
public static Bitmap createAlignedBitmap(Bitmap bitmap, PrintAlignment alignment) {
|
||||||
if (!canPrint) {
|
if (bitmap.getWidth() >= IMAGE_WIDTH) {
|
||||||
LOG.error("Printer cannot print.");
|
return bitmap;
|
||||||
return;
|
} else {//handle alignment
|
||||||
|
Bitmap alignedBitmap = Bitmap.createBitmap(IMAGE_WIDTH, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
||||||
|
Canvas canvas = new Canvas(alignedBitmap);
|
||||||
|
canvas.drawColor(Color.WHITE);
|
||||||
|
|
||||||
|
switch (alignment) {
|
||||||
|
case ALIGN_LEFT -> canvas.drawBitmap(bitmap, 0, 0, new Paint());
|
||||||
|
case ALIGN_CENTER ->
|
||||||
|
canvas.drawBitmap(bitmap, (alignedBitmap.getWidth() - bitmap.getWidth()) / 2, 0, new Paint());
|
||||||
|
case ALIGN_RIGHT ->
|
||||||
|
canvas.drawBitmap(bitmap, alignedBitmap.getWidth() - bitmap.getWidth(), 0, new Paint());
|
||||||
|
}
|
||||||
|
return alignedBitmap;
|
||||||
}
|
}
|
||||||
final BitmapToBitSet bitmapToBitSet = new BitmapToBitSet(bitmap);
|
}
|
||||||
|
|
||||||
final ByteBuffer result = ByteBuffer.allocate(500 + (int) (Math.ceil(bitmap.getWidth() / 8) + 8) * bitmap.getHeight()); //TODO: approximate better?
|
private void sendTestPrint() {
|
||||||
|
printImage(createBitmapFromString(ZonedDateTime.now().toString()), false, PrintAlignment.ALIGN_CENTER);
|
||||||
result.put(PrinterCommand.intensity(PRINT_INTENSITY));
|
|
||||||
result.put(PrinterCommand.printSpeed.message(new byte[]{PRINT_SPEED}));
|
|
||||||
result.put(PrinterCommand.printType.message(new byte[]{PRINT_TYPE}));
|
|
||||||
result.put(PrinterCommand.quality(5));
|
|
||||||
|
|
||||||
result.put(encodePictureToPrinterCommands(bitmapToBitSet.toBlackAndWhite(applyDithering), bitmapToBitSet.getWidth(), bitmapToBitSet.getHeight()));
|
|
||||||
result.put(PrinterCommand.feedPaper(72));
|
|
||||||
|
|
||||||
result.put(PrinterCommand.getDevState.message(new byte[]{0})); //to get back the current device status
|
|
||||||
|
|
||||||
send("Print...", byteBufferToArray(result));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -380,27 +406,6 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(printCommandReceiver);
|
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(printCommandReceiver);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void sendTestPrint() {
|
|
||||||
Bitmap bitmap = BitmapFactory.decodeResource(GBApplication.app().getResources(), R.drawable.ic_launcher);
|
|
||||||
|
|
||||||
Bitmap newBitmap = Bitmap.createBitmap(IMAGE_WIDTH, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
|
||||||
Canvas canvas = new Canvas(newBitmap);
|
|
||||||
canvas.drawColor(Color.WHITE);
|
|
||||||
|
|
||||||
// Calculate the top-left coordinates for centering the original Bitmap
|
|
||||||
int posLeft = 0;
|
|
||||||
int posMiddle = (newBitmap.getWidth() - bitmap.getWidth()) / 2;
|
|
||||||
int posRight = newBitmap.getWidth() - bitmap.getWidth();
|
|
||||||
|
|
||||||
int left = posRight;
|
|
||||||
int top = (newBitmap.getHeight() - bitmap.getHeight()) / 2;
|
|
||||||
|
|
||||||
canvas.drawBitmap(bitmap, left, top, new Paint());
|
|
||||||
|
|
||||||
canvas.drawText(ZonedDateTime.now().toString(), newBitmap.getHeight(), 0, new Paint());
|
|
||||||
|
|
||||||
printImage(newBitmap, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSendConfiguration(String config) {
|
public void onSendConfiguration(String config) {
|
||||||
@@ -438,6 +443,12 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor
|
|||||||
//we just abuse the install functionality to start our own activity
|
//we just abuse the install functionality to start our own activity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum PrintAlignment {
|
||||||
|
ALIGN_LEFT,
|
||||||
|
ALIGN_CENTER,
|
||||||
|
ALIGN_RIGHT
|
||||||
|
}
|
||||||
|
|
||||||
private enum PrinterCommand {
|
private enum PrinterCommand {
|
||||||
feedPaper(0xa1),
|
feedPaper(0xa1),
|
||||||
printRowPlain(0xa2),
|
printRowPlain(0xa2),
|
||||||
|
|||||||
@@ -18,41 +18,7 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/sendToPrinterButton"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginStart="24dp"
|
|
||||||
android:layout_marginTop="8dp"
|
|
||||||
android:layout_marginEnd="24dp"
|
|
||||||
android:text="@string/activity_print_image_print_button"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintHorizontal_bias="0.0"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toBottomOf="@+id/warning_devices" />
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/incomingImage"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginStart="24dp"
|
|
||||||
android:layout_marginTop="16dp"
|
|
||||||
android:layout_marginEnd="24dp"
|
|
||||||
android:adjustViewBounds="true"
|
|
||||||
android:scaleType="fitCenter"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/imageSizeText"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginTop="4dp"
|
|
||||||
android:text="@string/activity_print_label_print_size"
|
|
||||||
app:layout_constraintTop_toBottomOf="@+id/incomingImage"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent" />
|
|
||||||
|
|
||||||
<com.google.android.material.materialswitch.MaterialSwitch
|
<com.google.android.material.materialswitch.MaterialSwitch
|
||||||
android:id="@+id/switchUpscale"
|
android:id="@+id/switchUpscale"
|
||||||
@@ -62,7 +28,7 @@
|
|||||||
android:layout_marginTop="8dp"
|
android:layout_marginTop="8dp"
|
||||||
android:layout_marginEnd="24dp"
|
android:layout_marginEnd="24dp"
|
||||||
android:text="@string/activity_print_image_upscale_switch"
|
android:text="@string/activity_print_image_upscale_switch"
|
||||||
app:layout_constraintTop_toBottomOf="@id/imageSizeText"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent" />
|
app:layout_constraintEnd_toEndOf="parent" />
|
||||||
|
|
||||||
@@ -91,6 +57,17 @@
|
|||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/switchDithering" />
|
app:layout_constraintTop_toBottomOf="@+id/switchDithering" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/imageSizeText"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="4dp"
|
||||||
|
android:text="@string/activity_print_label_print_size"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/convertedImage"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent" />
|
||||||
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/warning_devices"
|
android:id="@+id/warning_devices"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
@@ -98,9 +75,21 @@
|
|||||||
android:layout_marginTop="8dp"
|
android:layout_marginTop="8dp"
|
||||||
android:layout_marginBottom="8dp"
|
android:layout_marginBottom="8dp"
|
||||||
android:text=""
|
android:text=""
|
||||||
app:layout_constraintTop_toBottomOf="@+id/convertedImage"
|
app:layout_constraintTop_toBottomOf="@+id/imageSizeText"
|
||||||
tools:layout_editor_absoluteX="0dp" />
|
tools:layout_editor_absoluteX="0dp" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/sendToPrinterButton"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="24dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:layout_marginEnd="24dp"
|
||||||
|
android:text="@string/activity_print_image_print_button"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintHorizontal_bias="0.0"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/warning_devices" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user