This repository was archived by the owner on Jul 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathRNOpenCvLibraryModule.java
More file actions
82 lines (65 loc) · 2.89 KB
/
Copy pathRNOpenCvLibraryModule.java
File metadata and controls
82 lines (65 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.reactlibrary;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.android.Utils;
import org.opencv.imgproc.Imgproc;
import android.util.Base64;
public class RNOpenCvLibraryModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
public RNOpenCvLibraryModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RNOpenCvLibrary";
}
@ReactMethod
public void checkForBlurryImage(String imageAsBase64, Callback errorCallback, Callback successCallback) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
byte[] decodedString = Base64.decode(imageAsBase64, Base64.DEFAULT);
Bitmap image = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
// Bitmap image = decodeSampledBitmapFromFile(imageurl, 2000, 2000);
int l = CvType.CV_8UC1; //8-bit grey scale image
Mat matImage = new Mat();
Utils.bitmapToMat(image, matImage);
Mat matImageGrey = new Mat();
Imgproc.cvtColor(matImage, matImageGrey, Imgproc.COLOR_BGR2GRAY);
Bitmap destImage;
destImage = Bitmap.createBitmap(image);
Mat dst2 = new Mat();
Utils.bitmapToMat(destImage, dst2);
Mat laplacianImage = new Mat();
dst2.convertTo(laplacianImage, l);
Imgproc.Laplacian(matImageGrey, laplacianImage, CvType.CV_8U);
Mat laplacianImage8bit = new Mat();
laplacianImage.convertTo(laplacianImage8bit, l);
Bitmap bmp = Bitmap.createBitmap(laplacianImage8bit.cols(), laplacianImage8bit.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(laplacianImage8bit, bmp);
int[] pixels = new int[bmp.getHeight() * bmp.getWidth()];
bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
int maxLap = -16777216; // 16m
for (int pixel : pixels) {
if (pixel > maxLap)
maxLap = pixel;
}
// int soglia = -6118750;
int soglia = -8118750;
if (maxLap <= soglia) {
System.out.println("is blur image");
}
successCallback.invoke(maxLap <= soglia);
} catch (Exception e) {
errorCallback.invoke(e.getMessage());
}
}
}

