Dodaj activity obsługujące przypadki, w których kod nie został znaleziony wraz z podstawowym interfejsem; dodaj podstawowy preprocessing zrobionego zdjęcia za pomocą OpenCV; dodaj zakończenia działania activity w kilku miejscach

This commit is contained in:
Kacper Dudzic 2019-12-29 00:10:44 +01:00
parent e3f1ae3d5c
commit 6e7adc11a0
5 changed files with 133 additions and 5 deletions

View File

@ -12,6 +12,7 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".DecodeFail"></activity>
<activity android:name=".Decode" /> <activity android:name=".Decode" />
<activity android:name=".TakePhoto" /> <activity android:name=".TakePhoto" />
<activity android:name=".Help" /> <activity android:name=".Help" />

View File

@ -1,5 +1,6 @@
package com.example.lookifyv2; package com.example.lookifyv2;
import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
@ -23,11 +24,15 @@ import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils; import org.opencv.android.Utils;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
import static org.opencv.imgproc.Imgproc.ADAPTIVE_THRESH_MEAN_C;
import static org.opencv.imgproc.Imgproc.THRESH_BINARY;
public class Decode extends AppCompatActivity { public class Decode extends AppCompatActivity {
//inicjacja logów OpenCV //inicjacja logów OpenCV
@ -35,6 +40,9 @@ public class Decode extends AppCompatActivity {
String filePath; String filePath;
Mat matPic; Mat matPic;
Mat matPicG;
Mat matPicGB;
Mat matPicGBT;
//OpenCV domyślnie uruchamia się po onCreate co powoduje problemy - zapobiegamy więc temu //OpenCV domyślnie uruchamia się po onCreate co powoduje problemy - zapobiegamy więc temu
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@ -65,10 +73,22 @@ public class Decode extends AppCompatActivity {
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
filePath = extras.getString("takenPic"); filePath = extras.getString("takenPic");
matPic = Imgcodecs.imread(filePath); matPic = Imgcodecs.imread(filePath);
Mat matPicG = new Mat();
Mat matPicGB = new Mat();
Mat matPicGBT = new Mat();
String contents = null; //Zdjęcie zamieniane jest na czarno-biały odpowiednik
Bitmap bMap = Bitmap.createBitmap(matPic.width(), matPic.height(), Bitmap.Config.ARGB_8888); Imgproc.cvtColor(matPic, matPicG, Imgproc.COLOR_RGB2GRAY);
Utils.matToBitmap(matPic, bMap);
//Usuwanie "noise'u" przy jednoczesnym zachowywaniu ostrych krawędzi za pomocą bilateralnego filtru
Imgproc.bilateralFilter(matPicG, matPicGB, 5, 75, 75);
//Zwiększanie kontrastu za pomocą thresholdu(wartości adaptywnego zaczerpnięte z https://stackoverflow.com/questions/31289895/threshold-image-using-opencv-java)
//Imgproc.adaptiveThreshold(matPicGB, matPicGBT, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 40);
Imgproc.threshold(matPicGB, matPicGBT, 100, 255, THRESH_BINARY);
Bitmap bMap = Bitmap.createBitmap(matPicGBT.width(), matPicGBT.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(matPicGBT, bMap);
int[] intArray = new int[bMap.getWidth() * bMap.getHeight()]; int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
@ -100,13 +120,16 @@ public class Decode extends AppCompatActivity {
//Szukanie kodu na wczytanym zdjęciu //Szukanie kodu na wczytanym zdjęciu
MultiFormatReader reader = new MultiFormatReader(); MultiFormatReader reader = new MultiFormatReader();
String contents = null;
try { try {
Result result = reader.decode(bitmap, hints); Result result = reader.decode(bitmap, hints);
contents = result.getText(); contents = result.getText();
} catch (NotFoundException e) { } catch (NotFoundException e) {
e.printStackTrace(); e.printStackTrace();
Intent intent_DecodeFail = new Intent(this, DecodeFail.class);
startActivity(intent_DecodeFail);
Decode.this.finish();
} }
Toast toast = Toast.makeText(this, contents, Toast.LENGTH_LONG); Toast toast = Toast.makeText(this, contents, Toast.LENGTH_LONG);
toast.show(); toast.show();
} }

View File

@ -0,0 +1,47 @@
package com.example.lookifyv2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DecodeFail extends AppCompatActivity {
private Button button_tryagain;
private Button button_backtomain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_decode_fail);
button_tryagain = findViewById(R.id.try_again_button);
button_backtomain = findViewById(R.id.backto_main_button);
button_tryagain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openScan();
}
});
button_backtomain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Finish();
}
});
}
public void Finish(){
finish();
}
public void openScan(){
Intent intent_Scan = new Intent(this, TakePhoto.class);
startActivity(intent_Scan);
finish();
}
}

View File

@ -6,4 +6,15 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".Decode"> tools:context=".Decode">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please wait a second..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DecodeFail">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Whoops! Barcode could not be found!"
app:layout_constraintBottom_toTopOf="@+id/try_again_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/try_again_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Try again"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/backto_main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Return to menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/try_again_button"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>