2016-08-01 65 views
0

我正在嘗試整合anyline ocr sdk以從圖像中提取url鏈接。 從他們的documentation我明白,通過修改iban掃描儀的例子,我可以實現相同的結果爲url。Anyline OCR SDK集成,用於掃描圖像中的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(); 
     } 

    } 
} 

的AnylineOCRlistener無法儘管設定在65

信心來檢測任何的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示例。

+0

minConfidence你好,你設法從圖像中提取網址是什麼? – Dake

回答

0

其實有你遇到了兩個問題:

  1. 你只加了反斜槓\charWhitelist,而不是斜線/
  2. 你必須設置removeSmallContoursfalse

removeSmallContours從小於minCharHeight的行中刪除所有內容。所以在你的情況下,它將刪除:.,因爲它們對於SDK來說太小了。

改變這兩個設置後,掃描工作正常85.