代码语言:javascript复制转载请注明出处:小锋学长生活大爆炸[xfxuezhang.cn]
这原本是这位大佬发的(autojs调用zxing实现扫码功能_autojs扫码插件),略微修改了一下,方便自己的代码直接调用。
这里可以下载没有改动的源码:Autojs调用zxing实现条形码与二维码
下面是略微修改的源码:
代码语言:javascript复制"ui";
var utils = {};
// runtime.loadDex("core.dex");
runtime.loadDex("/sdcard/core.dex");
importClass(com.google.zxing.PlanarYUVLuminanceSource);
importClass(com.google.zxing.common.HybridBinarizer)
importClass(com.google.zxing.BinaryBitmap)
importClass(com.google.zxing.MultiFormatReader)
importClass(com.google.zxing.NotFoundException)
importClass(com.google.zxing.DecodeHintType)
importClass(com.google.zxing.BarcodeFormat)
var scanBinder = null;
var scanBindService = function () {
var intent = new android.content.Intent();
intent.setComponent(new android.content.ComponentName("com.ten.weixinpay", "com.ten.weixinpay.MyService"));
let listener = new JavaAdapter(android.content.ServiceConnection, {
onServiceConnected(componentName, iBinder) {
console.log("链接成功");
scanBinder = iBinder;
},
onServiceDisconnected(componentName) {
console.log("链接断开")
scanBinder = null;
scanBindService()
}
});
activity.bindService(intent, listener, 1)
}
//scanBindService();
var scan = function () {
if (!scanBinder) {
scanBindService();
};
if (!scanBinder) {
toast("初始化中")
return;
}
var data = android.os.Parcel.obtain();
var result = android.os.Parcel.obtain();
scanBinder.transact(1, data, result, 0);
console.log("返回");
result.readException();
return result.readString();
}
var show = function (callback) {
var dialog_win = floaty.rawWindow(
<vertical>
<button id="back">返回</button>
<android.view.SurfaceView id="surfaceView" />
</vertical>
);
dialog_win.setSize(device.width/2, device.height/2);
dialog_win.setPosition(device.width/4, device.height/4);
dialog_win.back.click(function () {
callback(null);
dialog_win.close();
return;
})
var mCamera;
var surfaceHolder = dialog_win.surfaceView.getHolder();
//90度
var rotateCW = function (data2, imageWidth, imageHeight) {
var yuv = util.java.array('byte', imageWidth * imageHeight);
var i = 0;
for (var x = 0; x < imageWidth; x ) {
for (var y = imageHeight - 1; y >= 0; y--) {
yuv[i] = data2[(y * imageWidth) x];
i ;
}
}
return yuv;
}
var size = null;
var stateHandler = new android.os.Handler(new JavaAdapter(android.os.Handler.Callback,{
handleMessage(message) {
console.log(message);
if(message.what == 1){
console.log(message.obj);
}
return true;
}
}))
var setCallback = function () {
mCamera.setOneShotPreviewCallback(new JavaAdapter(android.hardware.Camera.PreviewCallback, {
onPreviewFrame(data, camera) {
// stateHandler.obtainMessage(1, data).sendToTarget();
try {
var source = new com.google.zxing.PlanarYUVLuminanceSource(data, size.width, size.height, 0, 0, size.width, size.height, false);
if (!source) {
return;
}
var bitmap = new com.google.zxing.BinaryBitmap(new com.google.zxing.common.HybridBinarizer(source));
if (!bitmap) {
return;
}
var reader = new com.google.zxing.MultiFormatReader();
var m = new java.util.HashMap()
m.put(com.google.zxing.DecodeHintType.CHARACTER_SET, "utf-8")
var rawResult = reader.decode(bitmap, m);
if (rawResult) {
callback(rawResult.getText());
dialog_win.close();
return;
}
} catch (error) {
//console.log(error);
}
setCallback();
}
}))
}
var focusing = false;
var autoFocusCallback = new android.hardware.Camera.AutoFocusCallback({
onAutoFocus(success, theCamera) {
focusing = false;
setAutoFocus()
//setCallback();
}
})
var setAutoFocus = function(){
if(!focusing){
focusing = true;
mCamera.autoFocus(autoFocusCallback);
}
}
surfaceHolder.addCallback(new JavaAdapter(android.view.SurfaceHolder.Callback, {
surfaceCreated: function (surfaceDestroyed) {
mCamera = android.hardware.Camera.open(0); // 0 或者 1
if (!mCamera) {
toast("摄像头打开失败");
return
}
try {
var parameters = mCamera.getParameters();
size = parameters.getPreviewSize();
decodeFormats = parameters.getPreviewFormat();
var focusModes = parameters.getSupportedFocusModes();
if (focusModes.contains(android.hardware.Camera.Parameters.FOCUS_MODE_AUTO)) {
parameters.setFocusMode(android.hardware.Camera.Parameters.FOCUS_MODE_AUTO);
}
mCamera.setParameters(parameters)
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(surfaceHolder);
} catch (error) {
console.log(error);
}
},
surfaceChanged: function (surfaceDestroyed, arg1, arg2, arg3) {
mCamera.startPreview();
setAutoFocus();
setCallback();
},
surfaceDestroyed: function (surfaceDestroyed) {
focusing = false;
if (mCamera) {
try {
mCamera.release();
} catch (error) {}
}
}
}))
}
utils.show = show;
module.exports = utils;
调用示例:
代码语言:javascript复制var barcode = require("./barcode.js");
barcode.show(function (result) {
if(result) {
console.log("识别到的条形码: " result);
// ui.barcodeInput.setText(result);
// showFloatWindow(searchDatabase(result));
}else {
console.log("没有识别到条形码");
}
});
我用来做了一个扫码查询价格的小工具,实现效果:
演示视频:https://live.csdn.net/v/363594
apk体验下载链接:https://xfxuezhang.lanzouo.com/izWFA1ofrl3c