NextQRCode ZXing開源庫的精簡版
基於ZXing Android實現生成二維碼圖片和相機掃描二維碼圖片即時解碼的功能 原文部落格 附原始碼下載地址
與原ZXingMini專案對比
NextQRCode做了重大架構修改,原ZXingMini專案與當前NextQRCode不相容
dependencies {
compile 'com.github.yoojia:next-qrcode:2.0-2'
...
}
複製程式碼
生成二維碼圖案
// 二維碼中間圖示
final Bitmap centerImage = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
// 生成的二維碼圖案
Bitmap qrCodeImage = new QRCodeEncoder.Builder()
.width(size) // 二維碼圖案的寬度
.height(size)
.paddingPx(0) // 二維碼的內邊距
.marginPt(3) // 二維碼的外邊距
.centerImage(centerImage) // 二維碼中間圖示
.build()
.encode(content);
複製程式碼
二維碼解碼
final QRCodeDecoder mDecoder = new QRCodeDecoder.Builder().build();
// 傳入二維碼圖案Bitmap物件然後解碼成文字內容
String content = mDecoder.decode(bitmap);
複製程式碼
使用 LiveCameraView 自動掃描解碼
public class QRCodeScanActivity extends AppCompatActivity {
public static final String TAG = QRCodeScanActivity.class.getSimpleName();
private LiveCameraView mLiveCameraView;
private ImageView mCaptureImage;
private TextView mContentView;
private final CaptureCallback mCaptureCallback = new CaptureCallback() {
@Override public void onCaptured(Bitmap bitmap) {
Log.i(TAG, "-> Got bitmap, show to capture view");
mCaptureImage.setImageBitmap(bitmap);
Observable.just(bitmap)
.map(new Func1<Bitmap, String>() {
private final QRCodeDecoder mDecoder = new QRCodeDecoder.Builder().build();
@Override
public String call(Bitmap bitmap) {
return mDecoder.decode(bitmap);
}
})
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
@Override public void call(String content) {
mContentView.setText(content);
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_scan);
mCaptureImage = (ImageView) findViewById(R.id.capture_image);
mContentView = (TextView) findViewById(R.id.content);
mLiveCameraView = (LiveCameraView) findViewById(R.id.capture_preview_view);
mLiveCameraView.setPreviewReadyCallback(new CameraPreviewView.PreviewReadyCallback() {
@Override
public void onStarted(Camera camera) {
Log.i(TAG, "-> Camera started, start to auto capture");
mLiveCameraView.startAutoCapture(1500, mCaptureCallback);
}
@Override
public void onStopped() {
Log.i(TAG, "-> Camera stopped");
mLiveCameraView.stopAutoCapture();
}
});
}
}
複製程式碼
基於ZXing Android實現生成二維碼圖片和相機掃描二維碼圖片即時解碼的功能 原文部落格 附原始碼下載地址