181 lines
6.7 KiB
Java
181 lines
6.7 KiB
Java
package com.example.lookifyv2;
|
|
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.PorterDuff;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.BinaryBitmap;
|
|
import com.google.zxing.DecodeHintType;
|
|
import com.google.zxing.LuminanceSource;
|
|
import com.google.zxing.MultiFormatReader;
|
|
import com.google.zxing.NotFoundException;
|
|
import com.google.zxing.RGBLuminanceSource;
|
|
import com.google.zxing.Result;
|
|
import com.google.zxing.common.HybridBinarizer;
|
|
|
|
import org.opencv.android.BaseLoaderCallback;
|
|
import org.opencv.android.LoaderCallbackInterface;
|
|
import org.opencv.android.OpenCVLoader;
|
|
import org.opencv.android.Utils;
|
|
import org.opencv.core.Mat;
|
|
import org.opencv.imgcodecs.Imgcodecs;
|
|
import org.opencv.imgproc.Imgproc;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.Hashtable;
|
|
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 {
|
|
|
|
//inicjacja logów OpenCV
|
|
private static final String TAG = "OCVSample::Activity";
|
|
|
|
String filePath;
|
|
Mat matPic;
|
|
Mat matPicG;
|
|
Mat matPicGB;
|
|
Mat matPicGBT;
|
|
|
|
//OpenCV domyślnie uruchamia się po onCreate co powoduje problemy - zapobiegamy więc temu
|
|
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
|
|
@Override
|
|
public void onManagerConnected(int status) {
|
|
switch (status) {
|
|
case LoaderCallbackInterface.SUCCESS: {
|
|
Log.i(TAG, "OpenCV loaded successfully");
|
|
decodePic();
|
|
}
|
|
break;
|
|
default: {
|
|
super.onManagerConnected(status);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_decode);
|
|
}
|
|
|
|
public void decodePic() {
|
|
|
|
CheckInternet check = new CheckInternet();
|
|
if(!check.isOnline()){
|
|
Bundle extras = getIntent().getExtras();
|
|
filePath = extras.getString("takenPic");
|
|
File filefordeletion = new File(filePath);
|
|
filefordeletion.delete();
|
|
|
|
Intent intent_DecodeFail = new Intent(this, DecodeFail.class);
|
|
intent_DecodeFail.putExtra("status", "offline");
|
|
startActivity(intent_DecodeFail);
|
|
finish();
|
|
}
|
|
|
|
//Pobieranie adresu pliku przekazanego przez poprzednie activity, tworzenie mata ze zdjęcia i usuwanie zdjęcia
|
|
Bundle extras = getIntent().getExtras();
|
|
filePath = extras.getString("takenPic");
|
|
matPic = Imgcodecs.imread(filePath);
|
|
File filefordeletion = new File(filePath);
|
|
filefordeletion.delete();
|
|
Mat matPicG = new Mat();
|
|
Mat matPicGB = new Mat();
|
|
Mat matPicGBT = new Mat();
|
|
|
|
//Zdjęcie zamieniane jest na czarno-biały odpowiednik
|
|
Imgproc.cvtColor(matPic, matPicG, Imgproc.COLOR_RGB2GRAY);
|
|
|
|
//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()];
|
|
|
|
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
|
|
LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
|
|
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
|
|
|
//Zwiększanie efektywności wykrywania kodów
|
|
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
|
|
|
|
List<Object> formats = new ArrayList<>();
|
|
formats.add(BarcodeFormat.AZTEC);
|
|
formats.add(BarcodeFormat.CODABAR);
|
|
formats.add(BarcodeFormat.CODE_39);
|
|
formats.add(BarcodeFormat.CODE_93);
|
|
formats.add(BarcodeFormat.CODE_128);
|
|
formats.add(BarcodeFormat.DATA_MATRIX);
|
|
formats.add(BarcodeFormat.EAN_8);
|
|
formats.add(BarcodeFormat.EAN_13);
|
|
formats.add(BarcodeFormat.ITF);
|
|
formats.add(BarcodeFormat.PDF_417);
|
|
formats.add(BarcodeFormat.QR_CODE);
|
|
formats.add(BarcodeFormat.RSS_14);
|
|
formats.add(BarcodeFormat.RSS_EXPANDED);
|
|
formats.add(BarcodeFormat.UPC_A);
|
|
formats.add(BarcodeFormat.UPC_E);
|
|
|
|
hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);
|
|
hints.put(DecodeHintType.TRY_HARDER, true);
|
|
|
|
int reminder = 0;
|
|
//Szukanie kodu na wczytanym zdjęciu
|
|
MultiFormatReader reader = new MultiFormatReader();
|
|
String contents = null;
|
|
try {
|
|
Result result = reader.decode(bitmap, hints);
|
|
contents = result.getText();
|
|
} catch (NotFoundException e) {
|
|
reminder = -1;
|
|
e.printStackTrace();
|
|
Intent intent_DecodeFail = new Intent(this, DecodeFail.class);
|
|
intent_DecodeFail.putExtra("status", "notfound");
|
|
startActivity(intent_DecodeFail);
|
|
finish();
|
|
}
|
|
if(reminder == 0) {
|
|
|
|
Toast.makeText(this, "Zidentyfikowany kod: " + contents,
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
Intent intent_Results = new Intent(this, Results.class);
|
|
intent_Results.putExtra("decodedcode", contents);
|
|
startActivity(intent_Results);
|
|
finish();
|
|
}
|
|
}
|
|
|
|
//Druga część potrzebna do ładowania OpenCV w odpowiednim momencie
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
if (!OpenCVLoader.initDebug()) {
|
|
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
|
|
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
|
|
} else {
|
|
Log.d(TAG, "OpenCV library found inside package. Using it!");
|
|
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
|
|
}
|
|
}
|
|
} |