Commit 9767c156 authored by Martin LOISEAU's avatar Martin LOISEAU 🎄
Browse files

Set QR capture as a different activity

parent f6f7b79f
......@@ -22,6 +22,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".QRCaptureActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
\ No newline at end of file
......@@ -15,6 +15,7 @@ import com.example.qrquest.QR.model.QRCode;
import com.example.qrquest.exceptions.UnhandledQRException;
import com.example.qrquest.exceptions.UnsupportedQRException;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.Result;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
......@@ -23,6 +24,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.gms.common.GoogleApiAvailability;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
......@@ -50,20 +52,6 @@ public class MainActivity extends AppCompatActivity {
SpeechRecognizer sr;
GoogleApiAvailability m_gga;
// attributes from QRLudo:
CameraSource m_cameraSource;
SurfaceView m_cameraView;
private int cameraState;
private final int CAMERA_INACTIVE_STATE = 30;
private final int CAMERA_DETECTING_STATE = 40;
private int m_detectionProgress;
public final static int NO_QR_DETECTED = 50;
/**
* The main layout where all happens
*/
private RelativeLayout m_mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -76,6 +64,8 @@ public class MainActivity extends AppCompatActivity {
tts.setLanguage(Locale.FRENCH);
}
});
Log.d("Debug2", "App launched");
m_gga = GoogleApiAvailability.getInstance();
......@@ -85,33 +75,11 @@ public class MainActivity extends AppCompatActivity {
Dialog errorDia = m_gga.getErrorDialog(this,status, GOOGLE_SERVICES_REQUEST);
errorDia.show();
}
initializeAttributes();
setUpDetector();
initializeListeners();
}
// From QRLudo
/**
* Starts the detection of new QRCodes
*/
private void startDetection() {
final AppCompatActivity activity = this;
activity.runOnUiThread(new Runnable() {
public void run() {
m_cameraView.setVisibility(View.VISIBLE);
}
});
}
private void initializeAttributes() {
m_cameraView = (SurfaceView) findViewById(R.id.camera_view);
m_cameraView.setVisibility(View.INVISIBLE);
PackageManager manager = getPackageManager();
m_mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
cameraState = CAMERA_INACTIVE_STATE;
m_detectionProgress = NO_QR_DETECTED;
}
private void initializeListeners() {
......@@ -120,9 +88,10 @@ public class MainActivity extends AppCompatActivity {
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Debug2", "Photo button pressed");
Snackbar.make(view, "Should open QR code reader", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
startDetection();
DemoQR();
}
});
......@@ -215,20 +184,31 @@ public class MainActivity extends AppCompatActivity {
return true;
}
public boolean DemoQR()
{
Log.d("Debug2", "Demo QR launch");
requestPhotoPermission();
Log.d("Debug2", "Photo permission requested");
Intent PhotoIntent = new Intent( this, QRCaptureActivity.class);
Log.d("Debug2", "Photo intent created");
//startActivity(PhotoIntent);
startActivityForResult(PhotoIntent, 1);
Log.d("Debug2", "Photo activity launched");
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
public void SetListeningResults(String result) // to display listening results
{
Log.d("Debug", "Best confidence result (from main activity): " + result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
public void SetQRContent(String qr_result) // to display listening results
{
//end detection
m_cameraView.setVisibility(View.INVISIBLE);
Log.d("Debug", "QR content (from main activity): " + qr_result);
Toast.makeText(getApplicationContext(), qr_result, Toast.LENGTH_LONG).show();
}
private void requestRecordAudioPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
......@@ -253,129 +233,4 @@ public class MainActivity extends AppCompatActivity {
}
}
}
private void setUpDetector() {
requestPhotoPermission();
/*
The barcode detector
*/
BarcodeDetector detector = new BarcodeDetector.Builder(getApplicationContext())
.setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE)
.build();
if (!detector.isOperational()) {
Log.e("DETECTOR","Could not set up detector.");
return;
}
m_cameraSource = new CameraSource
.Builder(this, detector)
.setRequestedPreviewSize(640, 480)
.setAutoFocusEnabled(true)
.build();
m_cameraView = (SurfaceView) findViewById(R.id.camera_view);
m_cameraView.setVisibility(View.INVISIBLE);
m_cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
m_cameraSource.start(m_cameraView.getHolder());
cameraState = CAMERA_DETECTING_STATE;
Log.d("CAMERA_START", "Camera started");
} catch(SecurityException se)
{
Log.e("CAMERA SECURITY", se.getMessage());
}
catch (Exception e)
{
Log.e("CAMERA SOURCE", e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
try {
if(cameraState == CAMERA_DETECTING_STATE) {
cameraState = CAMERA_INACTIVE_STATE;
}
m_cameraSource.stop();
} catch (Exception e)
{
Log.e("CAMERA SOURCE", e.getMessage());
}
}
});
/*
The processor of the detector, where the events from the detector are handled
*/
Detector.Processor<Barcode> detector_processor = new Detector.Processor<Barcode>() {
ArrayList<QRCode> listQR = new ArrayList<>();
@Override
public void release() {
}
/**
* Method called when QRCodes have been detected by the camera
* @param detections
*/
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) { // no usages found in QRLudo, called by API
//Log.d("Debug", "receiveDetections() called");
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0)
{
if(barcodes.size() == 1) Log.d("Debug", "one barcode detected");
else Log.d("Debug", "several barcodes detected");
String rawValue = barcodes.valueAt(0).rawValue;
//ignoring if the QRCode has already been recorded or ignored
try {
QRCode detectedQR = QRCodeBuilder.build(rawValue);
// SetQRContent(rawValue);
listQR.add(detectedQR);
} catch (UnhandledQRException e) {
Log.d("Debug", "QR code non pris en charge");
} catch (UnsupportedQRException e) {
Log.d("Debug", "QR code non pris en charge");
}
}
}
};
detector.setProcessor(detector_processor);
}
/**
* Stops the detection : the application is no longer trying to detect QRCodes
* The cameraView becomes white
*/
public void stopDetection() {
final AppCompatActivity activity = this;
activity.runOnUiThread(new Runnable() {
public void run() {
if(true/*m_ttsready*/) {
m_cameraView.setVisibility(View.INVISIBLE);
}else{
cameraState = CAMERA_INACTIVE_STATE;
}
}
});
}
}
package com.example.qrquest.QR.model;
/**
* Created by Jules Leguy on 20/01/18.
*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Class whose role is to manage collections of QRCodes before they are read by the text to speech engine
*
*/
public class QRCodeCollection{
//Contains QRCodeComponents, which are QRCodeAtomique, QRCodeEnsemble
private LinkedList<QRCodeComponent> m_QRList;
//Contains the QRCodeComponents which have been detected but ignored (so that they can't be read anymore)
private LinkedList<QRCodeComponent> m_QRIgnoredList;
public QRCodeCollection(){
m_QRList = new LinkedList<>();
m_QRIgnoredList = new LinkedList<>();
}
public void addQR(QRCode qr) {
if (!isAlreadyInCollection(qr.getRawValue())) {
m_QRList.addLast(qr);
}
}
public void addIgnoredQR(QRCode qr){
if (!isAlreadyIgnored(qr.getRawValue())){
m_QRIgnoredList.add(qr);
}
}
/**
* returns a list of the content of the first QRCodeComponent of the list.
* @return
*/
public List<QRContent> getContentFirstQR(){
if (m_QRList.size()!=0)
return m_QRList.get(0).getQRContent();
else
return new ArrayList<QRContent>();
}
/**
* returns the list of all the content of all the QRCodeComponent of the list, except the first one
* @return
*/
public List<QRContent> getContentAllQRButFirst(){
ArrayList<QRContent> out = new ArrayList<QRContent>();
for (int i=1; i<m_QRList.size(); i++) {
out.addAll(m_QRList.get(i).getQRContent());
}
return out;
}
/**
* returns the list of all the content of all the QRCodeComponent of the list
* @return
*/
public List<QRContent> getContentAllQR(){
ArrayList<QRContent> out = new ArrayList<QRContent>();
for (QRCodeComponent qrComponent : m_QRList){
out.addAll(qrComponent.getQRContent());
}
return out;
}
/**
* Removes all the detected QRCodeComponents, including the ignored ones
*/
public void clear(){
m_QRList.clear();
m_QRIgnoredList.clear();
}
/**
* Removes the ignored QRCodeComponents
*/
public void clearIgnoredQRComponents(){
m_QRIgnoredList.clear();
}
/**
* returns true if the QRCode is already in the collection (as a QRCode or inside a FamilleQRCode)
* @param qrRawValue
* @return
*/
public boolean isAlreadyInCollection(String qrRawValue){
for (QRCodeComponent qrCodeComponent : m_QRList){
if (qrCodeComponent.contains(qrRawValue)){
return true;
}
}
return false;
}
public boolean isAlreadyHere(QRCodeComponent qrCodeComponent){
for(QRCodeComponent qr : m_QRList){
if (qr.equals(qrCodeComponent)){
return true;
}
}
return false;
}
/**
* returns true if the QRCode is already ignored
* @param qrRawValue
* @return
*/
public boolean isAlreadyIgnored(String qrRawValue){
for (QRCodeComponent qrCodeComponent : m_QRIgnoredList){
if (qrCodeComponent.contains(qrRawValue)){
return true;
}
}
return false;
}
/**
* Starts the downloading of each contained QRFile
* Called when the internet connection was off when the QRCodes have been detected and the connection has just been opened
*/
public void downloadAllFiles(){
for (QRCodeComponent qrCodeComponent : m_QRList){
qrCodeComponent.downloadQRFiles();
}
}
}
package com.example.qrquest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.qrquest.QR.handling.QRCodeBuilder;
import com.example.qrquest.QR.model.QRCode;
import com.example.qrquest.exceptions.UnhandledQRException;
import com.example.qrquest.exceptions.UnsupportedQRException;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.util.ArrayList;
public class QRCaptureActivity extends MainActivity {
// attributes from QRLudo:
CameraSource m_cameraSource;
SurfaceView m_cameraView;
private int cameraState;
private final int CAMERA_INACTIVE_STATE = 30;
private final int CAMERA_DETECTING_STATE = 40;
private int m_detectionProgress;
public final static int NO_QR_DETECTED = 50;
private RelativeLayout m_mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Debug2", "Creating QRCapture Activity");
setContentView(R.layout.photo_activity);
// Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
initializeAttributes();
setUpDetector();
startDetection();
Log.d("Debug2", "End of QRCapture activity creation");
}
/**
* Starts the detection of new QRCodes
*/
private void startDetection() {
final AppCompatActivity activity = this;
activity.runOnUiThread(new Runnable() {
public void run() {
m_cameraView.setVisibility(View.VISIBLE);
}
});
}
public void SetQRContent(String qr_result) // to display listening results
{
//end detection
m_cameraView.setVisibility(View.INVISIBLE);
Log.d("Debug", "QR content (from main activity): " + qr_result);
Toast.makeText(getApplicationContext(), qr_result, Toast.LENGTH_LONG).show();
}
private void initializeAttributes() {
m_cameraView = (SurfaceView) findViewById(R.id.camera_view);
m_cameraView.setVisibility(View.INVISIBLE);
PackageManager manager = getPackageManager();
m_mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
cameraState = CAMERA_INACTIVE_STATE;
m_detectionProgress = NO_QR_DETECTED;
}
private void setUpDetector() {
/*
The barcode detector
*/
BarcodeDetector detector = new BarcodeDetector.Builder(getApplicationContext())
.setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE)
.build();
if (!detector.isOperational()) {
Log.e("DETECTOR","Could not set up detector.");
return;
}
m_cameraSource = new CameraSource
.Builder(this, detector)
.setRequestedPreviewSize(640, 480)
.setAutoFocusEnabled(true)
.build();
m_cameraView = (SurfaceView) findViewById(R.id.camera_view);
m_cameraView.setVisibility(View.INVISIBLE);
m_cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
m_cameraSource.start(m_cameraView.getHolder());
cameraState = CAMERA_DETECTING_STATE;
Log.d("CAMERA_START", "Camera started");
} catch(SecurityException se)
{
Log.e("CAMERA SECURITY", se.getMessage());
}
catch (Exception e)
{
Log.e("CAMERA SOURCE", e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
try {
if(cameraState == CAMERA_DETECTING_STATE) {
cameraState = CAMERA_INACTIVE_STATE;
}
m_cameraSource.stop();
} catch (Exception e)
{
Log.e("CAMERA SOURCE", e.getMessage());
}
}
});
/*
The processor of the detector, where the events from the detector are handled
*/
Detector.Processor<Barcode> detector_processor = new Detector.Processor<Barcode>() {
ArrayList<QRCode> listQR = new ArrayList<>();
@Override
public void release() {
}
/**
* Method called when QRCodes have been detected by the camera
* @param detections