-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from bendyworks/image-orientation
Image orientation
- Loading branch information
Showing
13 changed files
with
199 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,61 @@ | ||
package com.bendyworks.capML; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.Matrix; | ||
import android.net.Uri; | ||
import android.provider.MediaStore; | ||
|
||
import com.getcapacitor.JSObject; | ||
import com.getcapacitor.NativePlugin; | ||
import com.getcapacitor.Plugin; | ||
import com.getcapacitor.PluginCall; | ||
import com.getcapacitor.PluginMethod; | ||
import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
@NativePlugin() | ||
public class CapML extends Plugin { | ||
|
||
@PluginMethod() | ||
public void detectText(PluginCall call) { | ||
public void detectText(PluginCall call) throws IOException { | ||
String filename = call.getString("filename"); | ||
if (filename == null) { | ||
call.reject("filename not specified"); | ||
return; | ||
} | ||
// remove file:// from the filename | ||
filename = filename.substring(7); | ||
File file = new File(filename); | ||
String orientation = call.getString("orientation"); | ||
int rotation = this.orientationToRotation(orientation); | ||
|
||
if (file.exists()) { | ||
Uri uri = Uri.fromFile(file); | ||
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContext().getContentResolver(), Uri.parse(filename)); | ||
if (bitmap == null) { | ||
call.reject("Could not load image from path"); | ||
return; | ||
} else { | ||
int width = bitmap.getWidth(); | ||
int height = bitmap.getHeight(); | ||
|
||
Matrix matrix = new Matrix(); | ||
matrix.setRotate((float)rotation); | ||
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); | ||
|
||
TextDetector td = new TextDetector(); | ||
td.detectText(call, this.getContext(), uri, 0); | ||
} else { | ||
call.reject("File not found"); | ||
return; | ||
td.detectText(call, rotatedBitmap); | ||
} | ||
} | ||
|
||
private int orientationToRotation(String orientation) { | ||
switch (orientation) { | ||
case "UP": | ||
return 0; | ||
case "RIGHT": | ||
return 90; | ||
case "DOWN": | ||
return 180; | ||
case "LEFT": | ||
return 270; | ||
default: | ||
return 0; | ||
} | ||
} | ||
} |
36 changes: 17 additions & 19 deletions
36
android/src/main/java/com/bendyworks/capML/TextDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.