0

我正在尝试集成anyline ocr sdk 以从图像中提取 url 链接。从他们的文档中我了解到,通过修改 iban 扫描仪示例,我可以为 url 获得相同的结果。

这是扫描视图的配置文件

{
  "captureResolution":"1080",
  "cutout": {
    "style": "rect",
    "maxWidthPercent": "80%",
    "maxHeightPercent": "80%",
    "alignment": "top_half",
    "width": 900,
    "ratioFromSize": {
      "width": 10,
      "height": 1
    },
    "strokeWidth": 2,
    "cornerRadius": 10,
    "strokeColor": "FFFFFF",
    "outerColor": "000000",
    "outerAlpha": 0.3,
    "feedbackStrokeColor": "0099FF"
  },
  "flash": {
    "mode": "manual",
    "alignment": "bottom_right"
  },
  "beepOnResult": true,
  "vibrateOnResult": true,
  "blinkAnimationOnResult": true,
  "cancelOnResult": true,
  "visualFeedback": {
    "style": "contour_point",
    "strokeColor": "0099FF",
    "strokeWidth": 2,
    "fillColor": "110099FF"
  }
}

ScanURLActivity.java 的代码

public class ScanURLActivity extends AppCompatActivity {
    private static final String TAG = ScanURLActivity.class.getSimpleName();
    private AnylineOcrScanView scanView;
    private URLResultView urlResultView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Set the flag to keep the screen on (otherwise the screen may go dark during scanning)
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_anyline_ocr);

        addURLResultView();

        String license = getString(R.string.anyline_license_key);

        // Copies given traineddata-file to a place where the core can access it.
        // This MUST be called for every traineddata file that is used (before startScanning() is called).
        // The file must be located directly in the assets directory (or in tessdata/ but no other folders are allowed)
        scanView = new AnylineOcrScanView(getApplicationContext(),null);
        scanView.copyTrainedData("tessdata/eng_no_dict.traineddata", "d142032d86da1be4dbe22dce2eec18d7");
        scanView.copyTrainedData("tessdata/deu.traineddata", "2d5190b9b62e28fa6d17b728ca195776");

        //Configure the OCR for URLs
        AnylineOcrConfig anylineOcrConfig = new AnylineOcrConfig();
        // use the line mode (line length and font may vary)
        anylineOcrConfig.setScanMode(AnylineOcrConfig.ScanMode.LINE);
        // set the languages used for OCR
        anylineOcrConfig.setTesseractLanguages("eng_no_dict", "deu");
        // allow only capital letters and numbers
        anylineOcrConfig.setCharWhitelist("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.\\:");
        // set the height range the text can have
        anylineOcrConfig.setMinCharHeight(20);
        anylineOcrConfig.setMaxCharHeight(60);
        // The minimum confidence required to return a result, a value between 0 and 100.
        // (higher confidence means less likely to get a wrong result, but may be slower to get a result)
        anylineOcrConfig.setMinConfidence(65);
        // a simple regex for a basic validation of the URL, results that don't match this, will not be returned
        // (full validation is more complex, as different countries have different formats)
        anylineOcrConfig.setValidationRegex("^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$");
        // removes small contours (helpful in this case as no letters with small artifacts are allowed, like iöäü)
        anylineOcrConfig.setRemoveSmallContours(true);
        // removes whitespaces from the result
        // (also causes faster processing, because optimizations can be made if whitespaces are not relevant)
        anylineOcrConfig.setRemoveWhitespaces(true);
        // Experimental parameter to set the minimum sharpness (value between 0-100; 0 to turn sharpness detection off)
        // The goal of the minimum sharpness is to avoid a time consuming ocr step,
        // if the image is blurry and good results are therefor not likely.
        anylineOcrConfig.setMinSharpness(66);
        // set the ocr config
        scanView.setAnylineOcrConfig(anylineOcrConfig);

        // set an individual focus configuration for this example
        FocusConfig focusConfig = new FocusConfig.Builder()
                .setDefaultMode(Camera.Parameters.FOCUS_MODE_AUTO) // set default focus mode to be auto focus
                .setAutoFocusInterval(8000) // set an interval of 8 seconds for auto focus
                .setEnableFocusOnTouch(true) // enable focus on touch functionality
                .setEnablePhaseAutoFocus(true)  // enable phase focus for faster focusing on new devices
                .setEnableFocusAreas(true)  // enable focus areas to coincide with the cutout
                .build();
        // set the focus config
        scanView.setFocusConfig(focusConfig);
        // set the highest possible preview fps range
        scanView.setUseMaxFpsRange(true);
        // set sports scene mode to try and bump up the fps count even more
        scanView.setSceneMode(Camera.Parameters.SCENE_MODE_SPORTS);

        // initialize with the license and a listener
        scanView.initAnyline(license, new AnylineOcrListener() {
            @Override
            public void onReport(String identifier, Object value) {
                // Called with interesting values, that arise during processing.
                // Some possibly reported values:
                //
                // $brightness - the brightness of the center region of the cutout as a float value
                // $confidence - the confidence, an Integer value between 0 and 100
                // $thresholdedImage - the current image transformed into black and white
                // $sharpness - the detected sharpness value (only reported if minSharpness > 0)
            }

            @Override
            public boolean onTextOutlineDetected(List<PointF> list) {
                // Called when the outline of a possible text is detected.
                // If false is returned, the outline is drawn automatically.
                return false;
            }

            @Override
            public void onResult(AnylineOcrResult result) {
                // Called when a valid result is found (minimum confidence is exceeded and validation with regex was ok)
                urlResultView.setResult(result.getText());
                urlResultView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAbortRun(AnylineOcrError code, String message) {
                // Is called when no result was found for the current image.
                // E.g. if no text was found or the result is not valid.
            }
        });

        // disable the reporting if set to off in preferences
        if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
                SettingsFragment.KEY_PREF_REPORTING_ON, true)) {
            // The reporting of results - including the photo of a scanned meter -
            // helps us in improving our product, and the customer experience.
            // However, if you wish to turn off this reporting feature, you can do it like this:
            scanView.setReportingEnabled(false);
        }
        urlResultView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                urlResultView.setVisibility(View.INVISIBLE);
                scanView.startScanning();
            }
        });

    }
    private void addURLResultView() {
        RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

        urlResultView = new URLResultView(this);
        urlResultView.setVisibility(View.INVISIBLE);

        mainLayout.addView(urlResultView, params);
    }

    @Override
    protected void onResume() {
        super.onResume();

        scanView.startScanning();
    }

    @Override
    protected void onPause() {
        super.onPause();

        scanView.cancelScanning();
        scanView.releaseCameraInBackground();
    }

    @Override
    public void onBackPressed() {
        if (urlResultView.getVisibility() == View.VISIBLE) {
            urlResultView.setVisibility(View.INVISIBLE);
            scanView.startScanning();
        } else {
            super.onBackPressed();
        }

    }
}

尽管置信度设置为 65,AnylineOCRlistener 仍无法检测到任何 ocr 结果。

URLResultView.java 类的代码

public class URLResultView extends RelativeLayout {

    private TextView resultText;

    public URLResultView(Context context) {
        super(context);
        init();
    }

    public URLResultView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public URLResultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {

        setPadding(DimensUtil.getPixFromDp(getContext(), 4), DimensUtil.getPixFromDp(getContext(), 16),
                DimensUtil.getPixFromDp(getContext(), 4), DimensUtil.getPixFromDp(getContext(), 16));

        //setBackgroundResource(R.drawable.);

        inflate(getContext(), R.layout.url_result, this);

        resultText = (TextView) findViewById(R.id.text_result);
    }

    public void setResult(String result) {
        resultText.setText(result.trim());
    }
}

有人可以帮助我进行此集成,因为除了他们的文档和 sdk 示例之外,我找不到任何其他资源/教程。

4

1 回答 1

-2

实际上,您遇到了两个问题:

  1. 您只将反斜杠添加\charWhitelist,而不是斜杠/
  2. 你必须设置removeSmallContoursfalse

removeSmallContours从行中删除小于minCharHeight. 因此,在您的情况下,它将删除:and .,因为它们对于 SDK 来说太小了。

更改这两个设置后,扫描工作正常,aminConfidence为 85。

于 2016-10-17T15:11:17.327 回答