2025-04-26 21:05:13 +08:00
using System ;
using System.Collections.Generic ;
using UnityEngine ;
namespace Runtime.Printer
{
public class HanYinPrinterManager : MonoBehaviour
{
public Texture2D logo ;
private static string GetTime ( )
{
string time = DateTime . Now . ToString ( "h:mm" ) ;
if ( DateTime . Now . Hour < 12 )
time + = "am" ;
else
time + = "pm" ;
return time ;
}
public void PrintTestPaper ( )
{
2025-05-23 16:24:00 +08:00
PrintLogo ( "192.168.0.126" , "123fdsa1" ,
2025-04-26 21:05:13 +08:00
"\"NOTEBOOK LG DOT HYDRANGEA BLUE HARD COVER,1,338,,Large Customization Front or Back,1,0,5577000008701,NOTEBOOK PK DOT MYRTLE GREEN HARD COVER,1,258,,Pocket Customization Front or Back,1,0,5577000008718\"" ,
123 , "abc1243" , "test" , "12345678" ) ;
}
void PrintText ( AndroidJavaClass printClass , string s )
{
printClass . CallStatic < int > ( "PrintText" , s , 0 , 0 , 0 ) ;
}
void PrintLine ( AndroidJavaClass printClass )
{
printClass . CallStatic < int > ( "PrintText" , " " , 0 , 4 , 0 ) ;
}
void PrintNewLine ( AndroidJavaClass printClass )
{
printClass . CallStatic < int > ( "PrintAndLineFeed" ) ;
}
/// <summary>
/// 打印
/// </summary>
/// <param name="printer_ip">打印机IP地址</param>
/// <param name="backend_order_id">这个应该就是orderID</param>
/// <param name="json">打印的主体内容</param>
/// <param name="totalPrice">价钱</param>
/// <param name="order_id">orderID</param>
/// <param name="customer_name">顾客姓名:指购买商品或服务的人的名字。</param>
/// <param name="customer_phone">顾客电话</param>
public void PrintLogo ( string printer_ip , string backend_order_id , string json , decimal totalPrice ,
string order_id , string customer_name , string customer_phone )
{
if ( Application . platform = = RuntimePlatform . Android )
{
try
{
// Get Android Context
using ( AndroidJavaClass unityPlayer = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" ) )
using ( AndroidJavaObject currentActivity =
unityPlayer . GetStatic < AndroidJavaObject > ( "currentActivity" ) )
{
// Call Print.PortOpen(Context, String)
using ( AndroidJavaClass printClass = new AndroidJavaClass ( "print.Print" ) )
{
string port = $"WiFi,{printer_ip},9100" ; // Change to your printer's connection type
int result = printClass . CallStatic < int > ( "PortOpen" , currentActivity , port ) ;
Debug . Log ( $"connect result: {result}" ) ;
}
}
using ( AndroidJavaClass printClass = new AndroidJavaClass ( "print.Print" ) )
{
printClass . CallStatic < int > ( "PrintText" , " \n" , 1 , 1 , 0 ) ;
using ( AndroidJavaClass wrapperClass = new AndroidJavaClass ( "PrinterWrapper" ) )
{
// Call PrintImage(String path, byte var1, byte var2, int var3)
byte [ ] array = logo . EncodeToPNG ( ) ;
int result = wrapperClass . CallStatic < int > ( "PrintImageFromString" , array , 0 , 0 ) ;
Debug . Log ( result ! = - 1 ? "Print logo success" : "Print logo failed" ) ;
}
PrintNewLine ( printClass ) ;
PrintNewLine ( printClass ) ;
PrintText ( printClass , "Code no. " + backend_order_id ) ;
PrintText ( printClass ,
"Date: " + DateTime . Now . ToString ( "yyyy/MM/dd" ) + " " + "Time: " + GetTime ( ) ) ;
PrintLine ( printClass ) ;
PrintNewLine ( printClass ) ;
json = json . Trim ( '\"' ) ;
string [ ] words = json . Split ( ',' ) ;
List < string > productNames = new List < string > ( ) ;
List < string > productQuantity = new List < string > ( ) ;
List < string > productPrice = new List < string > ( ) ;
List < string > productBarcode = new List < string > ( ) ;
for ( int i = 0 ; i < words . Length ; i + + )
{
if ( i % 4 = = 0 )
{
productNames . Add ( words [ i ] ) ;
}
else if ( i % 4 = = 1 )
{
productQuantity . Add ( words [ i ] ) ;
}
else if ( i % 4 = = 2 )
{
productPrice . Add ( words [ i ] ) ;
}
else if ( i % 4 = = 3 )
{
productBarcode . Add ( words [ i ] ) ;
}
}
int frontLimit = 30 ;
for ( int i = 0 ; i < productNames . Count ; i + + )
{
string productName = productNames [ i ] ;
int productLength = productName . Length ;
int head = 0 ; // Tracks current position in productName
while ( productLength > 0 )
{
int printLength = Math . Min ( frontLimit , productLength ) ;
string partToPrint ;
// Check if we are in the middle of a word
if ( head + printLength < productName . Length & &
! char . IsWhiteSpace ( productName [ head + printLength ] ) )
{
int lastSpace = productName . LastIndexOf ( ' ' , head + printLength , printLength ) ;
if ( lastSpace > head )
{
printLength = lastSpace - head ; // Adjust to break at the last space
}
}
partToPrint = productName . Substring ( head , printLength ) . Trim ( ) ;
// If it's the first line, append quantity and price
if ( head = = 0 )
{
partToPrint = partToPrint . PadRight ( frontLimit , ' ' ) +
( "x" + productQuantity [ i ] ) . PadLeft ( 3 , ' ' ) +
( "SGD" + productPrice [ i ] ) . PadLeft ( 10 , ' ' ) ;
}
else
{
partToPrint = partToPrint . PadRight ( frontLimit , ' ' ) ; // Pad remaining space
}
PrintText ( printClass , partToPrint ) ;
// Move to the next part
head + = printLength + 1 ; // Skip the space
productLength - = printLength + 1 ;
}
printClass . CallStatic < int > ( "PrintBarCode" , 69 , productBarcode [ i ] , 2 , 60 , 0 , 1 ) ;
PrintNewLine ( printClass ) ;
}
PrintLine ( printClass ) ;
PrintNewLine ( printClass ) ;
PrintText ( printClass ,
"Total:" . PadRight ( frontLimit , ' ' ) + ( "SGD" + totalPrice ) . PadLeft ( 13 , ' ' ) ) ;
PrintNewLine ( printClass ) ;
printClass . CallStatic < int > ( "PrintText" ,
"\nThank you! Please show this\nticket to the staff at the counter.\n\n" , 1 , 0 , 0 ) ;
printClass . CallStatic < int > ( "PrintText" , order_id , 1 , 0 , 32 ) ;
PrintNewLine ( printClass ) ;
PrintNewLine ( printClass ) ;
printClass . CallStatic < int > ( "PrintText" , customer_name + " " + customer_phone + "\n\n" , 1 , 0 ,
0 ) ;
PrintNewLine ( printClass ) ;
PrintNewLine ( printClass ) ;
PrintNewLine ( printClass ) ;
PrintNewLine ( printClass ) ;
printClass . CallStatic < int > ( "CutPaper" , 1 ) ;
printClass . CallStatic < bool > ( "PortClose" ) ;
}
}
catch ( Exception e )
{
Debug . LogError ( "Error calling PrintImage: " + e . Message ) ;
}
}
}
}
}