HarmonyOS library projects that provides easy to use and extensible Barcode Scanner views based on ZXing and ZBar.
Add the following dependency to your build.gradle file.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
implementation 'com.google.zxing:core:3.4.0'
compile project(path: ':zxing')
compile project(path: ':core')
}
1.) Add camera permission to your config.json file:
"reqPermissions": [
{
"name": "ohos.permission.CAMERA",
"reason": "open camera",
"usedScene":
{
"ability": ["me.dm7.barcodescanner.zxing.sample.MainAbility",],
"when": "always"
}
}
]
2.) A very basic ability would look like this:
public class SimpleScannerAbility extends Ability implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
public void onStart(Bundle state) {
super.onStart(state);
// Dynamiclly add ZXingScannerView to content_frame which represents a StackLayout
ComponentContainer contentFrame = (ComponentContainer) findComponentById(ResourceTable.Id_content_frame);
mScannerView = new ZXingScannerView(this);
contentFrame.addComponent(mScannerView);
}
@Override
public void onActive() {
super.onActive();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on active
}
@Override
public void onInActive() {
super.onInActive();
mScannerView.stopCamera(); // Stop camera on inactive
}
@Override
public void handleResult(Result rawResult) {
// Do something with the result here
HiLog.info(LOG_LABEL, "handleResult," + rawResult.getText());// Prints scan results
HiLog.info(LOG_LABEL, "handleResult," + rawResult.getBarcodeFormat().toString());// Prints the scan format (qrcode, pdf417 etc.)
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
}
Please take a look at the zxing-sample project for a full working example.
Take a look at the FullScannerAbility.java or FullScannerFraction.java classes to get an idea on advanced usage.
Interesting methods on the ZXingScannerView include:
// Toggle flash:
void setFlash(boolean);
// Toogle autofocus:
void setAutoFocus(boolean);
// Specify interested barcode formats:
void setFormats(List<BarcodeFormat> formats);
// Specify the cameraId to start with:
void startCamera(int cameraId);
Specify front-facing or rear-facing cameras by using the void startCamera(int cameraId);
method.
Supported Formats:
BarcodeFormat.UPC_A
BarcodeFormat.UPC_E
BarcodeFormat.EAN_13
BarcodeFormat.EAN_8
BarcodeFormat.RSS_14
BarcodeFormat.CODE_39
BarcodeFormat.CODE_93
BarcodeFormat.CODE_128
BarcodeFormat.ITF
BarcodeFormat.CODABAR
BarcodeFormat.QR_CODE
BarcodeFormat.DATA_MATRIX
BarcodeFormat.PDF_417
Add the following dependency to your build.gradle file.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har', '*.so'])
testCompile'junit:junit:4.12'
compile project(path: ':core')
}
1.) Add camera permission to your config.json file:
"reqPermissions": [
{
"name": "ohos.permission.CAMERA",
"reason": "open camera",
"usedScene":
{
"ability": ["me.dm7.barcodescanner.zbar.sample.MainAbility",],
"when": "always"
}
}
]
2.) A very basic activity would look like this:
public class SimpleScannerAbility extends Ability implements ZbarScannerView.ResultHandler {
private ZbarScannerView mScannerView;
@Override
public void onStart(Bundle state) {
super.onStart(state);
ComponentContainer contentFrame = (ComponentContainer) findComponentById(ResourceTable.Id_content_frame);
mScannerView = new ZbarScannerView(this);
contentFrame.addComponent(mScannerView);
}
@Override
public void onActive() {
super.onActive();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on active
}
@Override
public void onInActive() {
super.onInActive();
mScannerView.stopCamera(); // Stop camera on inactive
}
@Override
public void handleResult(Result rawResult) {
// Do something with the result here
HiLog.info(LOG_LABEL, "handleResult," + rawResult.getText());// Prints scan results
HiLog.info(LOG_LABEL, "handleResult," + rawResult.getBarcodeFormat().toString());// Prints the scan format (qrcode, pdf417 etc.)
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
}
Please take a look at the zbar-sample project for a full working example.
Take a look at the FullScannerAbility.java or FullScannerFraction.java classes to get an idea on advanced usage.
Interesting methods on the ZBarScannerView include:
// Toggle flash:
void setFlash(boolean);
// Toogle autofocus:
void setAutoFocus(boolean);
// Specify interested barcode formats:
void setFormats(List<BarcodeFormat> formats);
Specify front-facing or rear-facing cameras by using the void startCamera(int cameraId);
method.
Supported Formats:
BarcodeFormat.PARTIAL
BarcodeFormat.EAN8
BarcodeFormat.UPCE
BarcodeFormat.ISBN10
BarcodeFormat.UPCA
BarcodeFormat.EAN13
BarcodeFormat.ISBN13
BarcodeFormat.I25
BarcodeFormat.DATABAR
BarcodeFormat.DATABAR_EXP
BarcodeFormat.CODABAR
BarcodeFormat.CODE39
BarcodeFormat.PDF417
BarcodeFormat.QR_CODE
BarcodeFormat.CODE93
BarcodeFormat.CODE128
.so library is not well supported on current HarmonyOS version, so we can't include zbar dependency by har/jar, instead we need to import zbar so and jar to entry's libs folder as above.
Almost all of the code for these library projects is based on:
License for code written in this project is: Apache License, Version 2.0
License for zxing and zbar projects is here:
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Комментарии ( 0 )