gpt4 book ai didi

java - ANDROID 扫描二维码后如何在新 Activity 中发送结果?

转载 作者:行者123 更新时间:2023-12-02 12:41:13 24 4
gpt4 key购买 nike

大家好,我正在尝试创建一个扫描二维码的应用程序。

在主 Activity 中,我想扫描二维码,然后在新 Activity 中发送结果。

这是我的代码

主要 Activity.java:

public class MainActivity extends AppCompatActivity {

SurfaceView cameraView;
BarcodeDetector barcode;
CameraSource cameraSource;
SurfaceHolder holder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, 200);
}




cameraView = (SurfaceView) findViewById(R.id.cameraView);
cameraView.setZOrderMediaOverlay(true);
holder = cameraView.getHolder();
barcode = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
if(!barcode.isOperational()){
Toast.makeText(getApplicationContext(), "Sorry, Couldn't setup the detector", Toast.LENGTH_LONG).show();
this.finish();
}
cameraSource = new CameraSource.Builder(this, barcode)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(24)
.setAutoFocusEnabled(true)
.setRequestedPreviewSize(1920,1024)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try{
if(ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
cameraSource.start(cameraView.getHolder());
}
}
catch (IOException e){
e.printStackTrace();
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
});
barcode.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {

}

@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if(barcodes.size() > 0){

Intent intent = new Intent(MainActivity.this, Result.class);
startActivityForResult(intent, 100);
intent.putExtra("barcode", barcodes.valueAt(0));
setResult(RESULT_OK, intent);
finish();
}
}
});
}}

结果.java:

public class Result extends AppCompatActivity {

TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
result = (TextView) findViewById(R.id.result);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 100 && resultCode == RESULT_OK){
if(data != null){
final Barcode barcode = data.getParcelableExtra("barcode");
result.post(new Runnable() {
@Override
public void run() {
result.setText(barcode.displayValue);
}
});
}
}
}}

但是当我尝试在结果 Activity 中测试应用程序时,我看不到结果。

怎么了?

谢谢大家

最佳答案

在 Result Activity 中,数据的检索应该在 onCreate 方法中进行,而不是在 onActivityResult 中,您的 Result 类应该如下所示:

public class Result extends AppCompatActivity {
TextView result;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
result = (TextView) findViewById(R.id.result);
final Barcode barcode = getIntent().getParcelableExtra("barcode");
result.setText(barcode.displayValue);
}
}

在 MainActivity 中,这一行:

intent.putExtra("barcode", barcodes.valueAt(0)); 

应该在

之前
startActivityForResult(intent, 100); 

不是之后,像这样:

Intent intent = new Intent(MainActivity.this, Result.class); 
intent.putExtra("barcode", barcodes.valueAt(0));
startActivityForResult(intent, 100);
finish();

关于java - ANDROID 扫描二维码后如何在新 Activity 中发送结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44925999/

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