Skip to content

Commit

Permalink
Added Label to ScanView, fixed Document Scanner Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas committed Jul 12, 2017
1 parent 578ae52 commit 19540f4
Show file tree
Hide file tree
Showing 33 changed files with 5,395 additions and 116 deletions.
2 changes: 2 additions & 0 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Tue Jul 11 10:17:12 CEST 2017
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
package com.anyline.reactnative;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -120,4 +124,39 @@ protected String jsonForOutline(List<PointF> pointList) {
return outline.toString();
}


protected TextView getLabelView(Context context) {

TextView labelView = new TextView(context);

try {
JSONObject jsonObject = new JSONObject(configJson);
JSONObject labelObject = jsonObject.getJSONObject("label");
labelView.setText(labelObject.getString("text"));
labelView.setTextColor(Color.parseColor("#" + labelObject.getString("color")));
labelView.setTextSize(Float.parseFloat(labelObject.getString("size")));
} catch (Exception e) {
e.printStackTrace();
}

return labelView;
}


protected RelativeLayout.LayoutParams getTextLayoutParams (){
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
return new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
}

protected RelativeLayout.LayoutParams getWrapContentLayoutParams (){
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
return lp;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
*/
package com.anyline.reactnative;

import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.graphics.Rect;
import android.view.View;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import at.nineyards.anyline.modules.ocr.AnylineOcrResultListener;
import at.nineyards.anyline.camera.CameraController;
import at.nineyards.anyline.camera.AnylineViewConfig;
import at.nineyards.anyline.modules.ocr.AnylineOcrConfig;
import at.nineyards.anyline.modules.ocr.AnylineOcrResult;
Expand All @@ -33,7 +37,8 @@ public class AnylineOcrActivity extends AnylineBaseActivity {
private static final String TAG = AnylineOcrActivity.class.getSimpleName();

private AnylineOcrScanView anylineOcrScanView;
private boolean drawTextOutline;
private TextView labelView;
private JSONObject viewConfig;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -42,15 +47,18 @@ protected void onCreate(Bundle savedInstanceState) {
String ocrConfigString = getIntent().getExtras().getString(AnylineSDKPlugin.EXTRA_OCR_CONFIG_JSON, "");

anylineOcrScanView = new AnylineOcrScanView(this, null);
RelativeLayout relativeLayout = new RelativeLayout(this);


try {
JSONObject json = new JSONObject(configJson);
anylineOcrScanView.setConfig(new AnylineViewConfig(this, json));
this.viewConfig = new JSONObject(configJson);
anylineOcrScanView.setConfig(new AnylineViewConfig(this, this.viewConfig));

if (json.has("reportingEnabled")) {
anylineOcrScanView.setReportingEnabled(json.optBoolean("reportingEnabled", true));
if (this.viewConfig.has("reportingEnabled")) {
anylineOcrScanView.setReportingEnabled(this.viewConfig.optBoolean("reportingEnabled", true));
}

json = new JSONObject(ocrConfigString);
JSONObject json = new JSONObject(ocrConfigString);

AnylineOcrConfig ocrConfig = new AnylineOcrConfig(json);
if (ocrConfig.getCustomCmdFile() != null) {
Expand Down Expand Up @@ -83,7 +91,8 @@ protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "No Training Data");
}

drawTextOutline = json.optBoolean("drawTextOutline", true);
// boolean drawTextOutline = json.optBoolean("drawTextOutline", true);


anylineOcrScanView.setAnylineOcrConfig(ocrConfig);

Expand All @@ -94,7 +103,15 @@ protected void onCreate(Bundle savedInstanceState) {
return;
}

setContentView(anylineOcrScanView);
relativeLayout.addView(anylineOcrScanView, getTextLayoutParams());

//add custom Label
if(this.viewConfig.has("label")){
this.labelView = getLabelView(getApplicationContext());
RelativeLayout.LayoutParams lp = getWrapContentLayoutParams();
relativeLayout.addView(this.labelView, lp);
}
setContentView(relativeLayout, getWrapContentLayoutParams());

initAnyline();
}
Expand All @@ -113,6 +130,31 @@ protected void onPause() {
anylineOcrScanView.releaseCameraInBackground();
}

@Override
public void onCameraOpened(CameraController cameraController, int width, int height) {
super.onCameraOpened(cameraController, width, height);
anylineOcrScanView.post(new Runnable() {
@Override
public void run() {
if (labelView != null) {
try {
Rect rect = anylineOcrScanView.getCutoutRect();
JSONObject offsetJson = new JSONObject(configJson).getJSONObject("label").getJSONObject("offset");


RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) labelView.getLayoutParams();
lp.setMargins(rect.left + Integer.parseInt(offsetJson.getString("x")), rect.top + Integer.parseInt(offsetJson.getString("y")), 0, 0);
labelView.setLayoutParams(lp);

labelView.setVisibility(View.VISIBLE);
} catch (JSONException e) {
finishWithError(e.toString());
}
}
}
});
}

private void initAnyline() {
anylineOcrScanView.setCameraOpenListener(this);

Expand Down
51 changes: 48 additions & 3 deletions android/src/main/java/com/anyline/reactnative/BarcodeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
* Created by martin at 2015-07-21
*/

import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -20,6 +24,7 @@
import java.util.UUID;

import at.nineyards.anyline.camera.AnylineViewConfig;
import at.nineyards.anyline.camera.CameraController;
import at.nineyards.anyline.models.AnylineImage;
import at.nineyards.anyline.modules.barcode.BarcodeResult;
import at.nineyards.anyline.modules.barcode.BarcodeResultListener;
Expand All @@ -30,21 +35,35 @@ public class BarcodeActivity extends AnylineBaseActivity {
private static final String TAG = BarcodeActivity.class.getSimpleName();

private BarcodeScanView barcodeScanView;
private TextView labelView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

barcodeScanView = new BarcodeScanView(this, null);
RelativeLayout relativeLayout = new RelativeLayout(this);


JSONObject viewConfig;
try {
JSONObject json = new JSONObject(configJson);
barcodeScanView.setConfig(new AnylineViewConfig(this, json));
viewConfig = new JSONObject(configJson);
barcodeScanView.setConfig(new AnylineViewConfig(this, viewConfig));
} catch (Exception e) {
//JSONException or IllegalArgumentException is possible, return it to javascript
finishWithError("error_invalid_json_data");
return;
}
setContentView(barcodeScanView);

relativeLayout.addView(barcodeScanView, getTextLayoutParams());

//add custom Label
if(viewConfig.has("label")){
this.labelView = getLabelView(getApplicationContext());
RelativeLayout.LayoutParams lp = getWrapContentLayoutParams();
relativeLayout.addView(this.labelView, lp);
}
setContentView(relativeLayout, getWrapContentLayoutParams());

initAnyline();
}
Expand Down Expand Up @@ -111,4 +130,30 @@ public void onResult(BarcodeResult result) {
barcodeScanView.getAnylineController().setWorkerThreadUncaughtExceptionHandler(this);
}


@Override
public void onCameraOpened(CameraController cameraController, int width, int height) {
super.onCameraOpened(cameraController, width, height);
barcodeScanView.post(new Runnable() {
@Override
public void run() {
if (labelView != null) {
try {
Rect rect = barcodeScanView.getCutoutRect();
JSONObject offsetJson = new JSONObject(configJson).getJSONObject("label").getJSONObject("offset");


RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) labelView.getLayoutParams();
lp.setMargins(rect.left + Integer.parseInt(offsetJson.getString("x")), rect.top + Integer.parseInt(offsetJson.getString("y")), 0, 0);
labelView.setLayoutParams(lp);

labelView.setVisibility(View.VISIBLE);
} catch (JSONException e) {
finishWithError(e.toString());
}
}
}
});
}

}
27 changes: 27 additions & 0 deletions android/src/main/java/com/anyline/reactnative/EnergyActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.gms.vision.barcode.Barcode;

Expand All @@ -44,6 +45,8 @@ public class EnergyActivity extends AnylineBaseActivity {
private RadioGroup radioGroup;
private AnylineUIConfig anylineUIConfig;
private String lastDetectedBarcodeValue;
private TextView labelView;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -144,6 +147,14 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
relativeLayout.addView(radioGroup, lp);
}


//add custom Label
if(jsonObject.has("label")){
this.labelView = getLabelView(getApplicationContext());
RelativeLayout.LayoutParams lp = getWrapContentLayoutParams();
relativeLayout.addView(this.labelView, lp);
}

setContentView(relativeLayout, matchParentParams);

initAnyline();
Expand Down Expand Up @@ -178,6 +189,22 @@ public void run() {

radioGroup.setVisibility(View.VISIBLE);
}

if (labelView != null) {
try {
Rect rect = energyScanView.getCutoutRect();
JSONObject offsetJson = new JSONObject(configJson).getJSONObject("label").getJSONObject("offset");


RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) labelView.getLayoutParams();
lp.setMargins(rect.left + Integer.parseInt(offsetJson.getString("x")), rect.top + Integer.parseInt(offsetJson.getString("y")), 0, 0);
labelView.setLayoutParams(lp);

labelView.setVisibility(View.VISIBLE);
} catch (JSONException e) {
finishWithError(e.toString());
}
}
}
});
}
Expand Down
Loading

0 comments on commit 19540f4

Please sign in to comment.