Skip to content

Instantly share code, notes, and snippets.

@aslamanver
Created January 10, 2023 10:10
Show Gist options
  • Save aslamanver/b12bcd56b481f4afbf1bf624c9d56bc3 to your computer and use it in GitHub Desktop.
Save aslamanver/b12bcd56b481f4afbf1bf624c9d56bc3 to your computer and use it in GitHub Desktop.
Example Demonstration of PAYable WPOS Printer SDK
package com.payable.posutils;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.payable.posutils.databinding.ActivityMainBinding;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class MainActivityLite extends AppCompatActivity {
final String PRINT_CALLBACK = "com.package.name.PRINT_CALLBACK";
ActivityMainBinding binding;
int count = 0;
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int result = intent.getIntExtra("PRINT_RESULT", 0);
String printId = intent.getStringExtra("PRINT_ID");
Toast.makeText(context, "PRINT_RESULT: " + result + " PRINT_ID: " + printId, Toast.LENGTH_LONG).show();
if (result == 0 /*On Success*/) {
if (count < 5) {
count++;
printBitmap();
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.btnPrint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
printBitmap();
}
});
registerReceiver(mReceiver, new IntentFilter(PRINT_CALLBACK));
binding.imageView.setImageBitmap(getBitmap("setImageBitmap", 1000));
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
private void printBitmap() {
Intent intent = new Intent("com.payable.posutils.POS_ACTION");
intent.putExtra("REQUEST", "PRINT");
intent.putExtra("PRINT_ID", "some_id");
intent.putExtra("PRINT_CALLBACK", PRINT_CALLBACK);
intent.putExtra("PRINT_SPACE", 60);
intent.putExtra("PRINT_IMAGE", convertBitmapToByteArray(getBitmap("printBitmap", 1000)));
sendBroadcast(intent);
}
private byte[] convertBitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private Bitmap getBitmap(String name, int height) {
Bitmap bitmap = Bitmap.createBitmap(400, height, Bitmap.Config.ARGB_8888);
Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
bitmap.eraseColor(Color.WHITE);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
paint.setColor(Color.BLACK);
paint.setTextSize(30);
int yposition = 30;
int space = 40;
for (int x = 0; yposition < height + space; x++) {
canvas.drawText(name + "-" + (x + 1), 5, yposition, paint);
yposition = yposition + space;
}
return bitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment