SUMIN扫码和打印总结
1 调用扫码驱动
1 调用sumins驱动代码如下:
private void initScan() {
Intent intent = new Intent("com.summi.scan");
intent.setPackage("com.sunmi.sunmiqrcodescanner");
intent.putExtra("CURRENT_PPI", 0X0003);//当前分辨率
//M1和V1的最佳是800*480,PPI_1920_1080 = 0X0001;PPI_1280_720 =
//0X0002;PPI_BEST = 0X0003;
intent.putExtra("PLAY_SOUND", true);// 扫描完成声音提示 默认true
intent.putExtra("PLAY_VIBRATE", false);
//扫描完成震动,默认false,目前M1硬件支持震动可用该配置,V1不支持
intent.putExtra("IDENTIFY_INVERSE_QR_CODE", true);// 识别反色二维码,默认true
intent.putExtra("IDENTIFY_MORE_CODE", false);// 识别画面中多个二维码,默认false
intent.putExtra("IS_SHOW_SETTING", true);// 是否显示右上角设置按钮,默认true
intent.putExtra("IS_SHOW_ALBUM", true);// 是否显示从相册选择图片按钮,默认true
startActivityForResult(intent, Decode);
}
2 扫码完毕后我们直接走的方法是onActivityResult()方法,我们在这里可以
获取到我们扫描的信息,对信息进行处理,代码如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && data != null) {
Bundle bundle = data.getExtras();
ArrayList<HashMap<String, String>> result = (ArrayList<HashMap<String, String>>) bundle
.getSerializable("data");
Iterator<HashMap<String, String>> it = result.iterator();
while (it.hasNext()) {
HashMap<String, String> hashMap = it.next();
String value = hashMap.get("VALUE");
String[] split = value.split("=");
orderId = split[1];
//在这进行网络请求,获取我们的数据,获取我们可使用工具的可用数量,如果可用数量是为0的话,我们就进行跳转
getAvalibaleCount(orderId);
}
}
}
2 打印接口初始化
只接代码:
/**
* 初始化打印回调接口
*/
private void initRPrint() {
callback = new ICallback.Stub() {
@Override
public void onRunResult(boolean isSuccess) throws RemoteException {
}
@Override
public void onReturnString(String result) throws RemoteException {
Log.i("Aaa", "printlength:" + value + "\n");
}
@Override
public void onRaiseException(int code, final String msg) throws RemoteException {
Log.i("aaa", "onRaiseException: " + msg);
runOnUiThread(new Runnable() {
@Override
public void run() {
// info.append("onRaiseException = " + msg + "\n");
}
});
}
};
init();
}
private void init() {
Intent intent = new Intent();
intent.setPackage("woyou.aidlservice.jiuiv5");
intent.setAction("woyou.aidlservice.jiuiv5.IWoyouService");
startService(intent);
bindService(intent, servicect2, Context.BIND_AUTO_CREATE);
}
ServiceConnection servicect2 = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
woyouService = IWoyouService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
woyouService = null;
}
};
3 调用驱动进行打印
Bitmap mBitmap;
private void rPrientTicket() {
ThreadPoolManager.getInstance().executeTask(new Runnable() {
@Override
public void run() {
if (mBitmap == null) {
mBitmap = BitmapFactory.decodeResource(getResources(), R.raw.test);
}
try {
//打印里面是前面是我们要打印的信息,后面是回调接口
woyouService.printText("产品名称:" + productNam + "\n", callback); //我需要进行传递
}
});
}
4 生成二维码
把我们要生成的二维码的数据,传递到我们的生成二维码图片的方法中
//展示二维码
private void showQR() {
ThreadPoolManager.getInstance().executeTask(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
//这里我们需要我们自己定义一个获取二维码图片,并显示到我们的imageview上
//第一个参数是我们扫描获取的数据
Bitmap bitmap = BytesUtil.createBitmap(value, 240);
iv_qr.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
5 生成二维码的线程池管理类
/**
* 线程管理类
*/
public class ThreadPoolManager {
private ExecutorService service;
private ThreadPoolManager() {
int num = Runtime.getRuntime().availableProcessors() * 20;
service = Executors.newFixedThreadPool(num);
}
private static final ThreadPoolManager manager = new
ThreadPoolManager();
public static ThreadPoolManager getInstance() {
return manager;
}
public void executeTask(Runnable runnable) {
service.execute(runnable);
}
}
生成二维码的工具包
//自己创建的生成二维码,显示在相应的imageview上
public static Bitmap createBitmap(String data,int size){
try {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//图像数据转换,使用了矩阵转换
BitMatrix bitMatrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, size, size, hints);
int[] pixels = new int[size * size];
//下面这里按照二维码的算法,逐个生成二维码的图片,
//两个for循环是图片横列扫描的结果
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
pixels[y * size + x] = 0xffffffff;
}
}
}
//生成二维码图片的格式,使用ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
总结:我们可以设置布局中的ImageView的宽高显示我们需要大小的二维码