Reputation: 119
Hello to everyone I'm trying to create an app that scan a QR Code.
In the main activity I want to make the scan of the QR Code and I want to send the result in a new Activity.
Here is my code
Main 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();
}
}
});
}}
Result.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);
}
});
}
}
}}
But when I try to test the app in the result activity i can't see the result.
What's wrong?
Thank you to everyone
Upvotes: 0
Views: 4478
Reputation: 170
the code You wrote in your MainActivity
Intent intent = new Intent(MainActivity.this, Result.class);
startActivityForResult(intent, 100);
intent.putExtra("barcode", barcodes.valueAt(0));
setResult(RESULT_OK, intent);
finish();
is wrong Actually it should be like the following and please do not ActivityForResult
Intent intent = new Intent(MainActivity.this, Result.class);
intent.putExtra("barcode", barcodes.valueAt(0));
startActivityForResult(intent, 100);
finish();
And for getting result from Your MainActivity in ResultActivity onCreate() method use following code if you are going to show result then
TextView YourResult
YourResult = (TextView) findViewById(R.id.TextView_YourResult);
final Barcode barcode = getIntent().getParcelableExtra("barcode");
result.setText(barcode.displayValue);
Upvotes: 0
Reputation: 9352
In Result activity the retrieving of the data should be in onCreate method, not in onActivityResult, your Result class should look like this:
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);
}
}
and in MainActivity, this line:
intent.putExtra("barcode", barcodes.valueAt(0));
should be before
startActivityForResult(intent, 100);
not after, like this:
Intent intent = new Intent(MainActivity.this, Result.class);
intent.putExtra("barcode", barcodes.valueAt(0));
startActivityForResult(intent, 100);
finish();
Upvotes: 1
Reputation: 585
You only use startActivityForResult
if you want the starting activity to be returned with an result from the called activity. In your case just start the result activity by calling startActivity
and get the data in the result activity by using getIntent
.
Upvotes: 0