一、配置ndk
这就不说了,网上一大堆,随便一搜就搞定了
二、下载zbar源码
1、下载Zbar、libiconv源码
到ZBar的Github托管主页上下载 https://github.com/ZBar/ZBar
下载libiconv,http://www.gnu.org/software/libiconv/#downloading
2、编译libiconv
编译libiconv需要在linux环境下,(libiconv已编译,无需重复此步骤)
3、编译zbar
解压zbar,
- 在项目中app/src/main/目录下新建jni文件夹
- 把编译好的libiconv放入我们项目的jni文件夹。
- 解压刚才下载好的Zbar,首先把Zbar的头文件所在文件夹zbar/include放入我们项目的jni文件夹下。
- 把Zbar对java的接口文件zbarjni.c放入我们项目的jni文件夹,zbrjni.c在zbar/java文件夹下。
- 把Zbar的核心库文件所在的文件夹zbar/zbar放到我们项目的jni文件夹下。
- 把Zbar编译时需要的Android.mk、Applicaiton.mk、config.h从zbar\android\jni下拷贝到我们项目的jni文件夹下
根据zbar目录结构,对Android.mk进行改动,主要改的是文件夹路径和文件路径,修改后的Android.mk的内容如下:
MY_LOCAL_PATH := $(call my-dir)
# libiconv
include $(CLEAR_VARS)
LOCAL_PATH := $(MY_LOCAL_PATH)
LOCAL_MODULE := libiconv
LOCAL_CFLAGS := \
-Wno-multichar \
-D_ANDROID \
-DLIBDIR="c" \
-DBUILDING_LIBICONV \
-DBUILDING_LIBCHARSET \
-DIN_LIBRARY
LOCAL_SRC_FILES := \
libiconv-1.15/lib/iconv.c \
libiconv-1.15/libcharset/lib/localcharset.c \
libiconv-1.15/lib/relocatable.c
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/libiconv-1.15/include \
$(LOCAL_PATH)/libiconv-1.15/libcharset \
$(LOCAL_PATH)/libiconv-1.15/libcharset/include
include $(BUILD_SHARED_LIBRARY)
LOCAL_LDLIBS := -llog -lcharset
# libzbarjni
include $(CLEAR_VARS)
LOCAL_PATH := $(MY_LOCAL_PATH)
LOCAL_MODULE := zbar
LOCAL_SRC_FILES := zbarjni.c \
zbar/img_scanner.c \
zbar/decoder.c \
zbar/image.c \
zbar/symbol.c \
zbar/convert.c \
zbar/config.c \
zbar/scanner.c \
zbar/error.c \
zbar/refcnt.c \
zbar/video.c \
zbar/video/null.c \
zbar/decoder/code128.c \
zbar/decoder/code39.c \
zbar/decoder/code93.c \
zbar/decoder/codabar.c \
zbar/decoder/databar.c \
zbar/decoder/ean.c \
zbar/decoder/i25.c \
zbar/decoder/qr_finder.c \
zbar/qrcode/bch15_5.c \
zbar/qrcode/binarize.c \
zbar/qrcode/isaac.c \
zbar/qrcode/qrdec.c \
zbar/qrcode/qrdectxt.c \
zbar/qrcode/rs.c \
zbar/qrcode/util.c
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/include \
$(LOCAL_PATH)/zbar \
$(LOCAL_PATH)/libiconv-1.15/include
LOCAL_SHARED_LIBRARIES := libiconv
include $(BUILD_SHARED_LIBRARY)
然后在Application.mk中填写你要编译的平台:
APP_ABI := armeabi armeabi-v7a x86
APP_PLATFORM := android-14
在app目录下build.gradle中添加
android {
…
…
sourceSets {
main {
jni.srcDirs = []
}
}
repositories {
flatDir {
dirs 'libs'
}
}
}
此时我们用命令行进入项目的jni文件夹的父目录,然后此时执行ndk-build进行编译。
三、运行
在app/src/main/目录下新建文件夹jniLibs,将libs中的文件拷贝到jniLibs中,
然后将解压后的zbar/java/net文件夹复制到app/src/main/java/下:
将解压后zbar/android/examples/CameraTest/src/net/sourceforge/zbar/android/CameraTest/下的CameraPreview.java类文件拷贝到主工程app/src/main/java/工程包名目录下:
将解压后zbar/android/examples/CameraTest/res/layout /下的main.xml文件拷贝到主工程中res/layout下
在MainActivity中调用.so库和native方法:
public class MainActivity extends AppCompatActivity {
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
TextView scanText;
Button scanButton;
ImageScanner scanner;
private boolean barcodeScanned = false;
private boolean previewing = true;
static {
System.loadLibrary("iconv");
System.loadLibrary("zbar");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
/* Instance barcode scanner */
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);
scanText = (TextView)findViewById(R.id.scanText);
scanButton = (Button)findViewById(R.id.ScanButton);
scanButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (barcodeScanned) {
barcodeScanned = false;
scanText.setText("Scanning...");
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
}
});
}
public void onPause() {
super.onPause();
releaseCamera();
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
} catch (Exception e){
}
return c;
}
private void releaseCamera() {
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
private Runnable doAutoFocus = new Runnable() {
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
String qrCodeString = null;
ImageScanner mImageScanner = new ImageScanner();
int result = mImageScanner.scanImage(barcode);
if (result != 0) {
SymbolSet symSet = mImageScanner.getResults();
for (Symbol sym : symSet)
qrCodeString = sym.getData();
}
if (!TextUtils.isEmpty(qrCodeString)) {
// 成功识别二维码,qrCodeString就是数据。
try {
String encodeResust = new String(qrCodeString.getBytes("GB2312"), "utf-8");
Toast.makeText(MainActivity.this, encodeResust, Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
};
// Mimic continuous auto-focusing
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
}
Demo地址:https://github.com/LeeVanie/ZbarDemo