Skip to content

Commit

Permalink
Implemented logic to scan and retrieve barcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaCostanzo1 committed Jun 9, 2019
1 parent 6da3bee commit d8a6928
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 32 deletions.
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".view.CameraActivity" android:theme="@style/beertastic_theme"/>
<activity android:name=".view.ScannerActivity" android:theme="@style/beertastic_theme"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;

import beertastic.sanag.com.flutter_beertastic.view.CameraActivity;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiFunction;
import java.util.function.Function;

import beertastic.sanag.com.flutter_beertastic.view.ScannerActivity;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
Expand All @@ -12,16 +25,47 @@ public class MainActivity extends FlutterActivity {

private final static String CAMERA_X_CHANNEL= "CAMERA_X";
private final static String SCAN_METHOD= "SCAN";
private int qrRequestCode;
private MethodChannel.Result result;
private SparseArray<BiFunction<Integer,Intent,Void>> resultHandlers;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getComponents();
setUpResultsHandlers();
new MethodChannel(getFlutterView(),CAMERA_X_CHANNEL).setMethodCallHandler((call, result)->{
if(call.method.equals(SCAN_METHOD)){
Intent intent = new Intent(this, CameraActivity.class);
startActivity(intent);
Intent intent = new Intent(this, ScannerActivity.class);
this.result=result;
startActivityForResult(intent,qrRequestCode);
}
});
GeneratedPluginRegistrant.registerWith(this);
}

private void getComponents() {
qrRequestCode=getResources().getInteger(R.integer.beertastic_qr_request_code);
}


private void setUpResultsHandlers() {
resultHandlers=new SparseArray<>();
resultHandlers.append(qrRequestCode,this::handleQrResult);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
resultHandlers.get(requestCode).apply(resultCode,data);
}

private Void handleQrResult(Integer resultCode, Intent intent) {
if(resultCode==RESULT_OK){
String qrData = intent.getStringExtra(getResources().getString(R.string.qr_code_data_extra));
Log.wtf("HAAHAHHAHAHAH",qrData);
result.success(qrData);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
Expand All @@ -21,31 +17,32 @@
import androidx.camera.core.CameraX;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageAnalysisConfig;
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.core.PreviewConfig;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProviders;

import com.google.android.gms.tasks.Task;
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.List;
import java.util.Optional;

import beertastic.sanag.com.flutter_beertastic.R;
import beertastic.sanag.com.flutter_beertastic.view_model.BarcodesScanner;
import beertastic.sanag.com.flutter_beertastic.view_model.ScannerViewModel;
import beertastic.sanag.com.flutter_beertastic.view_model.tools.BarcodesScanner;

public class CameraActivity extends AppCompatActivity implements LifecycleOwner {
public class ScannerActivity extends AppCompatActivity implements LifecycleOwner {

TextureView textureView;
LifecycleRegistry lifecycleRegistry;
private TextureView textureView;
private LifecycleRegistry lifecycleRegistry;
private ScannerViewModel viewModel;
private ImageAnalysis imageAnalysis;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -54,6 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
lifecycleRegistry = new LifecycleRegistry(this);
getComponents();
checkCameraPermission();
setUpViewModel();
lifecycleRegistry.markState(Lifecycle.State.CREATED);
}

Expand Down Expand Up @@ -131,32 +129,24 @@ private void setUpCameraX() {
.build();

int deviceRotation = getWindowManager().getDefaultDisplay().getRotation();
ImageAnalysis imageAnalysis = new ImageAnalysis(config);
imageAnalysis = new ImageAnalysis(config);

imageAnalysis.setAnalyzer(
(image, rotationDegrees) -> BarcodesScanner.getInstance().scanYUVImage(image.getPlanes()[0].getBuffer(), deviceRotation, this::handleScanResult));
(image, rotationDegrees) -> BarcodesScanner.getInstance()
.scanYUVImage(image.getPlanes()[0].getBuffer(), deviceRotation,
(barcodesList)->viewModel.handleScanResult(barcodesList)));

PreviewConfig previewConfig = new PreviewConfig.Builder().setTargetResolution(new Size(textureView.getWidth()/2, textureView.getHeight()/2)).build();
Preview preview = new Preview(previewConfig);


preview.setOnPreviewOutputUpdateListener(
previewOutput -> {
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
});
previewOutput -> textureView.setSurfaceTexture(previewOutput.getSurfaceTexture()));

CameraX.bindToLifecycle(this, imageAnalysis, preview);
}

private void handleScanResult(Task<List<FirebaseVisionBarcode>> listener) {
if (listener.isSuccessful()) {
Optional.ofNullable(listener.getResult())
.ifPresent(result -> {
if (result.size() > 0) Log.wtf("AHAHAHHAHA", result.get(0).getRawValue());
}
);
}
}



@NonNull
Expand All @@ -165,6 +155,19 @@ public Lifecycle getLifecycle() {
return lifecycleRegistry;
}

private void setUpViewModel() {
viewModel = ViewModelProviders.of(this).get(ScannerViewModel.class);
viewModel.getQrData().observe(this, this::setActivityResult);
}

private void setActivityResult(String qrData) {
CameraX.unbind(imageAnalysis);
Intent intent = new Intent();
intent.putExtra(getResources().getString(R.string.qr_code_data_extra),qrData);
setResult(RESULT_OK,intent);
finish();
}


// private Bitmap computeImage(ImageProxy image) {
// ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package beertastic.sanag.com.flutter_beertastic.view_model;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.android.gms.tasks.Task;
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode;

import java.util.List;
import java.util.Optional;

public class ScannerViewModel extends ViewModel {
private MutableLiveData<String> qrData;

public ScannerViewModel(){
qrData=new MutableLiveData<>();
}

public LiveData<String> getQrData() {
return qrData;
}

public void handleScanResult(Task<List<FirebaseVisionBarcode>> listener) {
if (listener.isSuccessful()) {
Optional.ofNullable(listener.getResult())
.ifPresent(result -> {
if (result.size() > 0) qrData.postValue(result.get(0).getRawValue());
}
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package beertastic.sanag.com.flutter_beertastic.view_model;
package beertastic.sanag.com.flutter_beertastic.view_model.tools;

import android.graphics.Bitmap;
import android.util.Log;
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/res/values/integers.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="beertastic_camera_request_code">200</integer>
<integer name="beertastic_qr_request_code">1000000001</integer>
</resources>
4 changes: 4 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="qr_code_data_extra">QrCode</string>
</resources>

0 comments on commit d8a6928

Please sign in to comment.