working of logo on top
This commit is contained in:
parent
49bedfdfcd
commit
b2ca2c1e47
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
|||||||
|
Lifeline-Test2
|
||||||
6
.idea/compiler.xml
generated
Normal file
6
.idea/compiler.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<bytecodeTargetLevel target="21" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
10
.idea/deploymentTargetSelector.xml
generated
Normal file
10
.idea/deploymentTargetSelector.xml
generated
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="deploymentTargetSelector">
|
||||||
|
<selectionStates>
|
||||||
|
<SelectionState runConfigName="app">
|
||||||
|
<option name="selectionMode" value="DROPDOWN" />
|
||||||
|
</SelectionState>
|
||||||
|
</selectionStates>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
13
.idea/deviceManager.xml
generated
Normal file
13
.idea/deviceManager.xml
generated
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DeviceTable">
|
||||||
|
<option name="columnSorters">
|
||||||
|
<list>
|
||||||
|
<ColumnSorterState>
|
||||||
|
<option name="column" value="Name" />
|
||||||
|
<option name="order" value="ASCENDING" />
|
||||||
|
</ColumnSorterState>
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/gradle.xml
generated
6
.idea/gradle.xml
generated
@ -7,6 +7,12 @@
|
|||||||
<option name="testRunner" value="CHOOSE_PER_TEST" />
|
<option name="testRunner" value="CHOOSE_PER_TEST" />
|
||||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
|
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
|
||||||
|
<option name="modules">
|
||||||
|
<set>
|
||||||
|
<option value="$PROJECT_DIR$" />
|
||||||
|
<option value="$PROJECT_DIR$/app" />
|
||||||
|
</set>
|
||||||
|
</option>
|
||||||
</GradleProjectSettings>
|
</GradleProjectSettings>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@ -1,7 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" default="true" project-jdk-name="24" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@ -26,6 +26,11 @@ android {
|
|||||||
sourceCompatibility JavaVersion.VERSION_11
|
sourceCompatibility JavaVersion.VERSION_11
|
||||||
targetCompatibility JavaVersion.VERSION_11
|
targetCompatibility JavaVersion.VERSION_11
|
||||||
}
|
}
|
||||||
|
|
||||||
|
packagingOptions {
|
||||||
|
pickFirst 'META-INF/LICENSE.md'
|
||||||
|
pickFirst 'META-INF/NOTICE.md'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@ -34,6 +39,7 @@ dependencies {
|
|||||||
implementation libs.material
|
implementation libs.material
|
||||||
implementation libs.activity
|
implementation libs.activity
|
||||||
implementation libs.constraintlayout
|
implementation libs.constraintlayout
|
||||||
|
implementation libs.databinding.compiler
|
||||||
testImplementation libs.junit
|
testImplementation libs.junit
|
||||||
androidTestImplementation libs.ext.junit
|
androidTestImplementation libs.ext.junit
|
||||||
androidTestImplementation libs.espresso.core
|
androidTestImplementation libs.espresso.core
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
@ -10,6 +10,9 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
|
android:usesCleartextTraffic="true"
|
||||||
|
android:networkSecurityConfig="@xml/network_security_config"
|
||||||
|
|
||||||
android:theme="@style/Theme.LifelineTest2">
|
android:theme="@style/Theme.LifelineTest2">
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
|
|||||||
@ -5,13 +5,22 @@ import android.app.PendingIntent;
|
|||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
import android.hardware.usb.UsbConstants;
|
import android.hardware.usb.UsbConstants;
|
||||||
import android.hardware.usb.UsbDevice;
|
import android.hardware.usb.UsbDevice;
|
||||||
import android.hardware.usb.UsbDeviceConnection;
|
import android.hardware.usb.UsbDeviceConnection;
|
||||||
import android.hardware.usb.UsbEndpoint;
|
import android.hardware.usb.UsbEndpoint;
|
||||||
import android.hardware.usb.UsbInterface;
|
import android.hardware.usb.UsbInterface;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
|
import android.util.DisplayMetrics;
|
||||||
|
|
||||||
|
import androidx.appcompat.content.res.AppCompatResources;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
@ -113,6 +122,111 @@ public class ESCPOS_Printer {
|
|||||||
send(new byte[]{0x1B, 0x40});
|
send(new byte[]{0x1B, 0x40});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void logo(Drawable drawable){
|
||||||
|
byte[] logoBytes = drawableToEscPosBytes(drawable, 324, 200);
|
||||||
|
send(logoBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] drawableToEscPosBytes(Drawable drawable, int newWidth, int newHeight) {
|
||||||
|
// Convert drawable to bitmap
|
||||||
|
Bitmap bitmap = drawableToBitmap(drawable);
|
||||||
|
|
||||||
|
// Resize if requested
|
||||||
|
if (newWidth > 0 && newHeight > 0) {
|
||||||
|
bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to monochrome bitmap
|
||||||
|
bitmap = toMonochrome(bitmap);
|
||||||
|
|
||||||
|
// Convert bitmap to ESC/POS image bytes
|
||||||
|
return bitmapToEscPosBytes(bitmap, true); // true = center align
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bitmap drawableToBitmap(Drawable drawable) {
|
||||||
|
if (drawable instanceof BitmapDrawable) {
|
||||||
|
return ((BitmapDrawable) drawable).getBitmap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Bitmap bitmap = Bitmap.createBitmap(
|
||||||
|
drawable.getIntrinsicWidth(),
|
||||||
|
drawable.getIntrinsicHeight(),
|
||||||
|
Bitmap.Config.ARGB_8888
|
||||||
|
);
|
||||||
|
Canvas canvas = new Canvas(bitmap);
|
||||||
|
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||||
|
drawable.draw(canvas);
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bitmap toMonochrome(Bitmap bitmap) {
|
||||||
|
int width = bitmap.getWidth();
|
||||||
|
int height = bitmap.getHeight();
|
||||||
|
Bitmap monoBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||||
|
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
int pixel = bitmap.getPixel(x, y);
|
||||||
|
int gray = (int) (0.3 * ((pixel >> 16) & 0xff) +
|
||||||
|
0.59 * ((pixel >> 8) & 0xff) +
|
||||||
|
0.11 * (pixel & 0xff));
|
||||||
|
if (gray < 128) {
|
||||||
|
monoBitmap.setPixel(x, y, 0xFF000000); // black
|
||||||
|
} else {
|
||||||
|
monoBitmap.setPixel(x, y, 0xFFFFFFFF); // white
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return monoBitmap;
|
||||||
|
}
|
||||||
|
private byte[] bitmapToEscPosBytes(Bitmap bitmap, boolean center) {
|
||||||
|
int width = bitmap.getWidth();
|
||||||
|
int height = bitmap.getHeight();
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
// Alignment
|
||||||
|
if (center) {
|
||||||
|
baos.write(0x1B); baos.write('a'); baos.write(1); // ESC a 1 -> center
|
||||||
|
} else {
|
||||||
|
baos.write(0x1B); baos.write('a'); baos.write(0); // ESC a 0 -> left
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set line spacing to 24 dots
|
||||||
|
baos.write(0x1B); baos.write(0x33); baos.write(24);
|
||||||
|
|
||||||
|
for (int y = 0; y < height; y += 24) {
|
||||||
|
baos.write(0x1B); baos.write('*'); baos.write(33); // 24-dot double-density
|
||||||
|
baos.write(width & 0xFF); // width low byte
|
||||||
|
baos.write((width >> 8) & 0xFF); // width high byte
|
||||||
|
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
for (int k = 0; k < 3; k++) {
|
||||||
|
byte slice = 0;
|
||||||
|
for (int b = 0; b < 8; b++) {
|
||||||
|
int yPos = y + k * 8 + b;
|
||||||
|
int pixelColor = 0xFFFFFF;
|
||||||
|
if (yPos < height) {
|
||||||
|
pixelColor = bitmap.getPixel(x, yPos);
|
||||||
|
}
|
||||||
|
if (pixelColor == 0xFF000000) { // black pixel
|
||||||
|
slice |= (1 << (7 - b));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
baos.write(slice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
baos.write(0x0A); // line feed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset line spacing
|
||||||
|
baos.write(0x1B); baos.write(0x32);
|
||||||
|
|
||||||
|
return baos.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void feed(Integer count){
|
public void feed(Integer count){
|
||||||
for(var i=0; i<=count; i++){
|
for(var i=0; i<=count; i++){
|
||||||
send(new byte[]{0x0A});
|
send(new byte[]{0x0A});
|
||||||
@ -137,7 +251,6 @@ public class ESCPOS_Printer {
|
|||||||
private void send(byte[] data) {
|
private void send(byte[] data) {
|
||||||
connection.bulkTransfer(endpointOut, data, data.length, 1000);
|
connection.bulkTransfer(endpointOut, data, data.length, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] hexStringToBytes(String hex) {
|
private byte[] hexStringToBytes(String hex) {
|
||||||
int len = hex.length();
|
int len = hex.length();
|
||||||
byte[] data = new byte[len / 2];
|
byte[] data = new byte[len / 2];
|
||||||
|
|||||||
@ -13,11 +13,14 @@ import android.hardware.usb.UsbInterface;
|
|||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.webkit.CookieManager;
|
||||||
import android.webkit.JavascriptInterface;
|
import android.webkit.JavascriptInterface;
|
||||||
import android.webkit.WebSettings;
|
import android.webkit.WebSettings;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
|
import android.webkit.WebViewClient;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.appcompat.content.res.AppCompatResources;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -49,23 +52,49 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
// protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
// super.onCreate(savedInstanceState);
|
||||||
|
//
|
||||||
|
// usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||||
|
//
|
||||||
|
// registerReceiver(usbReceiver, new IntentFilter(ACTION_USB_PERMISSION), RECEIVER_NOT_EXPORTED);
|
||||||
|
//
|
||||||
|
// WebView webView = new WebView(this);
|
||||||
|
// setContentView(webView);
|
||||||
|
// WebSettings webSettings = webView.getSettings();
|
||||||
|
// webSettings.setJavaScriptEnabled(true);
|
||||||
|
//
|
||||||
|
// webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPrinter");
|
||||||
|
// webView.loadUrl("http://192.168.0.114:8000/kioskvideo");
|
||||||
|
//// webView.loadUrl("file:///android_asset/index.html");
|
||||||
|
// findUsbPrinter();
|
||||||
|
// }
|
||||||
|
|
||||||
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
registerReceiver(usbReceiver, new IntentFilter(ACTION_USB_PERMISSION), RECEIVER_NOT_EXPORTED);
|
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||||
|
|
||||||
WebView webView = new WebView(this);
|
registerReceiver(usbReceiver, new IntentFilter(ACTION_USB_PERMISSION), RECEIVER_NOT_EXPORTED);
|
||||||
setContentView(webView);
|
|
||||||
WebSettings webSettings = webView.getSettings();
|
|
||||||
webSettings.setJavaScriptEnabled(true);
|
|
||||||
|
|
||||||
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPrinter");
|
WebView webView = new WebView(this);
|
||||||
webView.loadUrl("file:///android_asset/index.html");
|
setContentView(webView);
|
||||||
findUsbPrinter();
|
WebSettings webSettings = webView.getSettings();
|
||||||
}
|
webSettings.setJavaScriptEnabled(true);
|
||||||
|
webSettings.setDomStorageEnabled(true);
|
||||||
|
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidPrinter");
|
||||||
|
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
||||||
|
webView.setWebViewClient(new WebViewClient());
|
||||||
|
CookieManager.getInstance().setAcceptCookie(true);
|
||||||
|
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
|
||||||
|
// webView.loadUrl("http://10.23.107.10:8000/kioskvideo");
|
||||||
|
|
||||||
|
webView.loadUrl("http://192.168.0.114:8000/kioskvideo");
|
||||||
|
//webView.loadUrl("file:///android_asset/index.html");
|
||||||
|
findUsbPrinter();
|
||||||
|
}
|
||||||
|
|
||||||
private void findUsbPrinter(){
|
private void findUsbPrinter(){
|
||||||
// Find the first connected USB device
|
// Find the first connected USB device
|
||||||
@ -128,6 +157,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ESCPOS_Printer printer = new ESCPOS_Printer(usbDeviceConnection, usbEndpoint);
|
ESCPOS_Printer printer = new ESCPOS_Printer(usbDeviceConnection, usbEndpoint);
|
||||||
|
printer.logo(AppCompatResources.getDrawable(this, R.drawable.logo));
|
||||||
printer.printFormatted(text);
|
printer.printFormatted(text);
|
||||||
printer.feed(5);
|
printer.feed(5);
|
||||||
printer.cut();
|
printer.cut();
|
||||||
|
|||||||
BIN
app/src/main/res/drawable/logo.jpg
Normal file
BIN
app/src/main/res/drawable/logo.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 62 KiB |
4
app/src/main/res/xml/network_security_config.xml
Normal file
4
app/src/main/res/xml/network_security_config.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<network-security-config>
|
||||||
|
<base-config cleartextTrafficPermitted="true"/>
|
||||||
|
</network-security-config>
|
||||||
@ -7,6 +7,7 @@ appcompat = "1.7.1"
|
|||||||
material = "1.12.0"
|
material = "1.12.0"
|
||||||
activity = "1.10.1"
|
activity = "1.10.1"
|
||||||
constraintlayout = "2.2.1"
|
constraintlayout = "2.2.1"
|
||||||
|
databindingCompiler = "8.12.0"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||||
@ -16,6 +17,7 @@ appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "a
|
|||||||
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
||||||
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
|
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
|
||||||
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
|
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
|
||||||
|
databinding-compiler = { group = "androidx.databinding", name = "databinding-compiler", version.ref = "databindingCompiler" }
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user