我正在使用LivotovLabs的这个库CamView来扫描条形码。它按预期工作,但它不专注于特定的手机,所以我想实现对触摸的关注,我的解决方案如下。然而,它不起作用,手机不关注条形码。
camera = (ScannerLiveView) findViewById(R.id.camview);
camera.setFocusable(true);
camera.setFocusableInTouchMode(true);
camera.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
camera.requestFocus();
return true;
}
});
camera.setScannerViewEventListener(new ScannerLiveView.ScannerViewEventListener() {
@Override
public void onScannerStarted(ScannerLiveView scanner) {
}
@Override
public void onScannerStopped(ScannerLiveView scanner) {
}
@Override
public void onScannerError(Throwable err) {
Toast.makeText(ACME_Scanner.this, "Scanner Error: " + err.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCodeScanned(String data) {
Toast.makeText(ACME_Scanner.this, "Barcode No: "+data, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btnFlash).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleFlash();
}
});
findViewById(R.id.btnSkip).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
protected void onResume()
{
super.onResume();
ZXDecoder decoder = new ZXDecoder();
decoder.setScanAreaPercent(0.5);
camera.setDecoder(decoder);
camera.startScanner();
}
@Override
protected void onPause()
{
camera.stopScanner();
super.onPause();
}
public void toggleFlash()
{
try {
flashStatus = !flashStatus;
camera.getCamera().getController().switchFlashlight(flashStatus);
}
catch (NullPointerException e){
e.printStackTrace();
}
}