我正在寻找使用ZXing扫描仪的Android应用程序的一些帮助。如果结果是URL,我希望它重定向到一个网站。提前谢谢。
以下是我的代码:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView zXingScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
}
public void scan(View view) {
zXingScannerView = new ZXingScannerView(getApplicationContext());
setContentView(zXingScannerView);
zXingScannerView.setResultHandler(this);
zXingScannerView.startCamera();
}
public void onPause() {
super.onPause();
zXingScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
Toast.makeText(getApplicationContext(), result.getText(), Toast.LENGTH_SHORT).show();
zXingScannerView.resumeCameraPreview(this);
}
}
答案 0 :(得分:2)
在handleResult()
:
@Override
public void handleResult(Result result) {
if(Patterns.WEB_URL.matcher(result.getText).matches()) {
// Open URL
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(result.getText()));
startActivity(browserIntent);
}
}