gpt4 book ai didi

android - ZXING 代码阅读器中的自定义相机 View 以显示检测

转载 作者:行者123 更新时间:2023-11-29 21:03:45 25 4
gpt4 key购买 nike

我正在使用 ZXING 二维码阅读器。它工作正常。检测代码等,但在扫描二维码时不显示任何检测进度(如红线)。这是截图

enter image description here

我的 ScanActivity 是,

扫描代码.Java

public class ScanCode extends Activity implements OnClickListener {

Button btnScan;
TextView scanResult;
String text;
Bitmap bmp;
ImageView ivPic;

@Override
public void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.scan_code);
initViews();
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btnScanCode) {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1);
}
}

private void initViews() {
scanResult = (TextView) findViewById(R.id.scanResult);
ivPic = (ImageView) findViewById(R.id.capturedImg);
btnScan = (Button) findViewById(R.id.btnScanCode);
btnScan.setOnClickListener(this);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(getApplicationContext(), contents,
Toast.LENGTH_SHORT).show();
scanResult.setText(contents);
}// if result_ok
}// onActivityResult
}

最佳答案

我知道这个答案有点晚了,但我最近刚刚在我的应用程序中使用了 Zxing 条形码/QR 码扫描仪,如果有人在实现它时遇到问题,请按照以下步骤操作(使用 Android Studio 官方 IDE for android)。

首先在app --> build.gradle 文件中添加依赖

dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'
}

Activity 使用:

public class MainActivity extends AppCompatActivity {

Button btnScan;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnScan = (Button) findViewById(R.id.btnScan);
btnScan.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

scanBarcode();

}

});
}

private void scanBarcode()
{
new IntentIntegrator(this).initiateScan();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
}
}

}

如果你想使用自定义的回调和BarCodeView。 Zxing 提供此功能只需制作布局文件

<com.journeyapps.barcodescanner.CompoundBarcodeView
android:id="@+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent">

</com.journeyapps.barcodescanner.CompoundBarcodeView>

<TextView
android:id="@+id/tvScanResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan Results will be shown here"
android:textColor="@android:color/white"
android:textStyle="bold"/>

在 Activity 中使用以下代码

public class MainActivity extends AppCompatActivity
{

CompoundBarcodeView barcodeView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

barcodeView = (CompoundBarcodeView) findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
barcodeView.setStatusText("");

}

@Override
protected void onResume()
{
super.onResume();

barcodeView.resume();

isScanned = false;
}

@Override
protected void onPause()
{
super.onPause();

barcodeView.pause();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}

private BarcodeCallback callback = new BarcodeCallback()
{
@Override
public void barcodeResult(BarcodeResult result)
{
if (result.getText() != null)
{
barcodeView.setStatusText(result.getText());

tvscanResult.setText("Data found: " + result.getText());

}

//you can also Add preview of scanned barcode
//ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
//imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
}

@Override
public void possibleResultPoints(List<ResultPoint> resultPoints)
{
System.out.println("Possible Result points = " + resultPoints);
}
};
}

来源zxing-android-embedded .希望有人能从这个解决方案中得到帮助

关于android - ZXING 代码阅读器中的自定义相机 View 以显示检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25182294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com