Commit 599e6a7f authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

QR code reader

parent 685707be
......@@ -30,4 +30,5 @@ dependencies {
compile files('libs/jackson-core-2.9.2.jar')
compile files('libs/jackson-databind-2.9.2.jar')
compile files('libs/jackson-annotations-2.9.2.jar')
compile 'me.dm7.barcodescanner:zxing:1.9'
}
......@@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
......@@ -41,6 +42,9 @@
<activity android:name="fr.utc.simde.jessy.ArticleGroupActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name="fr.utc.simde.jessy.QRCodeReaderActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
</application>
</manifest>
\ No newline at end of file
......@@ -602,6 +602,19 @@ public abstract class BaseActivity extends InternetActivity {
return true;
}
protected boolean haveCameraPermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
return false;
}
}
else
return true;
}
protected void checkUpdate() { checkUpdate(true); }
protected void checkUpdate(final boolean popupIfNot) {
final Dialog updateDialog = new Dialog(BaseActivity.this);
......@@ -667,6 +680,13 @@ public abstract class BaseActivity extends InternetActivity {
}.start();
}
protected void startQRCodeReaderActivity(final Activity activity) {
if (haveCameraPermission())
startActivity(new Intent(activity, QRCodeReaderActivity.class));
else
dialog.errorDialog(BaseActivity.this, getString(R.string.qrcode), getString(R.string.need_camera_permission));
}
protected boolean update(final String version) {
final String destination = this.downloadLocation + getString(R.string.app_name) + " " + version + ".apk";
final String url = this.gitUrl + getString(R.string.app_name) + " " + version + ".apk";
......
......@@ -126,7 +126,7 @@ public class FoundationsOptionsActivity extends BaseActivity {
else if (isOption(position,3))
dialog.infoDialog(FoundationsOptionsActivity.this, "Non encore fait", "Pour la version 0.12");
else if (isOption(position,4))
dialog.infoDialog(FoundationsOptionsActivity.this, "Non encore fait", "Pour la version 0.10");
startQRCodeReaderActivity(FoundationsOptionsActivity.this);
else if (isOption(position,5))
startCardManagementActivity(FoundationsOptionsActivity.this);
else if (isOption(position,6))
......
package fr.utc.simde.jessy;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import com.google.zxing.Result;
import fr.utc.simde.jessy.tools.ExtendedScannerView;
import me.dm7.barcodescanner.core.IViewFinder;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import static android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK;
/**
* Created by Samy on 18/11/2017.
*/
public class QRCodeReaderActivity extends BaseActivity {
private static final String LOG_TAG = "_QRCodeReaderActivity";
protected ZXingScannerView scannerView;
protected ZXingScannerView.ResultHandler cameraHandler;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.scannerView = new ZXingScannerView(QRCodeReaderActivity.this) {
@Override
protected IViewFinder createViewFinderView(Context context) {
return new ExtendedScannerView(context);
}
};
setContentView(this.scannerView);
this.cameraHandler = new ZXingScannerView.ResultHandler() {
@Override
public void handleResult(Result result) {
Log.d(LOG_TAG, result.getText());
Log.d(LOG_TAG, result.getBarcodeFormat().toString());
dialog.infoDialog(QRCodeReaderActivity.this, getString(R.string.qrcode_reading), result.getText(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scannerView.resumeCameraPreview(cameraHandler);
}
});
}
};
this.scannerView.setResultHandler(this.cameraHandler);
this.scannerView.startCamera(CAMERA_FACING_BACK);
}
}
package fr.utc.simde.jessy.tools;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import fr.utc.simde.jessy.R;
import me.dm7.barcodescanner.core.DisplayUtils;
import me.dm7.barcodescanner.core.IViewFinder;
/**
* Created by Samy on 19/11/2017.
*/
public class ExtendedScannerView extends View implements IViewFinder {
private Rect mFramingRect;
private static final float PORTRAIT_WIDTH_RATIO = 0.9f;
private static final float PORTRAIT_WIDTH_HEIGHT_RATIO = 1.0f;
private static final float LANDSCAPE_HEIGHT_RATIO = 5f/8;
private static final float LANDSCAPE_WIDTH_HEIGHT_RATIO = 1.4f;
private static final int MIN_DIMENSION_DIFF = 50;
private static final float SQUARE_DIMENSION_RATIO = 0.9f;
private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
private int scannerAlpha;
private static final int POINT_SIZE = 10;
private static final long ANIMATION_DELAY = 80l;
private final int mDefaultLaserColor = getResources().getColor(R.color.viewfinder_laser);
private final int mDefaultMaskColor = getResources().getColor(R.color.viewfinder_mask);
private final int mDefaultBorderColor = getResources().getColor(R.color.viewfinder_border);
private final int mDefaultBorderStrokeWidth = getResources().getInteger(R.integer.viewfinder_border_width);
private final int mDefaultBorderLineLength = getResources().getInteger(R.integer.viewfinder_border_length);
protected Paint mFinderMaskPaint;
protected Paint mBorderPaint;
protected int mBorderLineLength;
protected boolean mSquareViewFinder;
public ExtendedScannerView(Context context) {
super(context);
init();
}
public ExtendedScannerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mFinderMaskPaint = new Paint();
mFinderMaskPaint.setColor(mDefaultMaskColor);
mBorderPaint = new Paint();
mBorderPaint.setColor(mDefaultBorderColor);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setStrokeWidth(mDefaultBorderStrokeWidth);
mBorderLineLength = mDefaultBorderLineLength;
}
public void setupViewFinder() {
updateFramingRect();
invalidate();
}
public Rect getFramingRect() {
return mFramingRect;
}
@Override
public void onDraw(Canvas canvas) {
if (getFramingRect() == null) {
return;
}
drawViewFinderMask(canvas);
drawViewFinderBorder(canvas);
drawLaser(canvas);
}
public void drawViewFinderMask(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
Rect framingRect = getFramingRect();
canvas.drawRect(0, 0, width, framingRect.top, mFinderMaskPaint);
canvas.drawRect(0, framingRect.top, framingRect.left, framingRect.bottom + 1, mFinderMaskPaint);
canvas.drawRect(framingRect.right + 1, framingRect.top, width, framingRect.bottom + 1, mFinderMaskPaint);
canvas.drawRect(0, framingRect.bottom + 1, width, height, mFinderMaskPaint);
}
public void drawViewFinderBorder(Canvas canvas) {
Rect framingRect = getFramingRect();
canvas.drawLine(framingRect.left - 1, framingRect.top - 1, framingRect.left - 1, framingRect.top - 1 + mBorderLineLength, mBorderPaint);
canvas.drawLine(framingRect.left - 1, framingRect.top - 1, framingRect.left - 1 + mBorderLineLength, framingRect.top - 1, mBorderPaint);
canvas.drawLine(framingRect.left - 1, framingRect.bottom + 1, framingRect.left - 1, framingRect.bottom + 1 - mBorderLineLength, mBorderPaint);
canvas.drawLine(framingRect.left - 1, framingRect.bottom + 1, framingRect.left - 1 + mBorderLineLength, framingRect.bottom + 1, mBorderPaint);
canvas.drawLine(framingRect.right + 1, framingRect.top - 1, framingRect.right + 1, framingRect.top - 1 + mBorderLineLength, mBorderPaint);
canvas.drawLine(framingRect.right + 1, framingRect.top - 1, framingRect.right + 1 - mBorderLineLength, framingRect.top - 1, mBorderPaint);
canvas.drawLine(framingRect.right + 1, framingRect.bottom + 1, framingRect.right + 1, framingRect.bottom + 1 - mBorderLineLength, mBorderPaint);
canvas.drawLine(framingRect.right + 1, framingRect.bottom + 1, framingRect.right + 1 - mBorderLineLength, framingRect.bottom + 1, mBorderPaint);
}
public void drawLaser(Canvas canvas) {
Rect framingRect = getFramingRect();
scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
postInvalidateDelayed(ANIMATION_DELAY,
framingRect.left - POINT_SIZE,
framingRect.top - POINT_SIZE,
framingRect.right + POINT_SIZE,
framingRect.bottom + POINT_SIZE);
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
updateFramingRect();
}
public synchronized void updateFramingRect() {
Point viewResolution = new Point(getWidth(), getHeight());
int width;
int height;
int orientation = DisplayUtils.getScreenOrientation(getContext());
if (mSquareViewFinder) {
if (orientation != Configuration.ORIENTATION_PORTRAIT) {
height = (int) (getHeight() * SQUARE_DIMENSION_RATIO);
width = height;
} else {
width = (int) (getWidth() * SQUARE_DIMENSION_RATIO);
height = width;
}
} else {
if(orientation != Configuration.ORIENTATION_PORTRAIT) {
height = (int) (getHeight() * LANDSCAPE_HEIGHT_RATIO);
width = (int) (LANDSCAPE_WIDTH_HEIGHT_RATIO * height);
} else {
width = (int) (getWidth() * PORTRAIT_WIDTH_RATIO);
height = (int) (PORTRAIT_WIDTH_HEIGHT_RATIO * width);
}
}
if (width > getWidth()) {
width = getWidth() - MIN_DIMENSION_DIFF;
}
if (height > getHeight()) {
height = getHeight() - MIN_DIMENSION_DIFF;
}
int leftOffset = (viewResolution.x - width) / 2;
int topOffset = (viewResolution.y - height) / 2;
mFramingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
}
}
\ No newline at end of file
......@@ -23,7 +23,6 @@
android:id="@+id/layout_articles"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:orientation="horizontal">
......
......@@ -59,6 +59,7 @@
<string name="type">Type</string>
<string name="day_s_">jour(s)</string>
<string name="update">Mise à jour</string>
<string name="qrcode">QR Code</string>
<string name="credit">Crédits</string>
<string name="author">&lt;/&gt; avec ♥ par Samy NASTUZZI</string>
......@@ -163,6 +164,8 @@
<string name="actual_version">Version actuelle</string>
<string name="available_version">Version disponible</string>
<string name="need_storage_permission">Il est nécessaire d\'autoriser l\'application à sauvegarder du contenu dans votre dossier de téléchargements</string>
<string name="need_camera_permission">Il est nécessaire d\'autoriser l\'application à utiliser la caméra pour lire un QR Code</string>
<string name="qrcode_reading">Lecture d\'un QR Code</string>
<string name="print_mode">Mode d\'affichage</string>
<string name="filters">Filtres d\'affichage</string>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment