OCRTest/src/main/java/edu/sfsu/cs/orange/ocr/CaptureActivity.java
boolean handleOcrDecode(OcrResult ocrResult) {
    lastResult = ocrResult;

    // Test whether the result is null
    if (ocrResult.getText() == null || ocrResult.getText()
            .equals("")) {
      Toast toast = Toast.makeText(this, "OCR failed. Please try again.", Toast.LENGTH_SHORT);
      toast.setGravity(Gravity.TOP, 0, 0);
      toast.show();
      return false;
    }

    // Turn off capture-related UI elements
    shutterButton.setVisibility(View.GONE);
    statusViewBottom.setVisibility(View.GONE);
    statusViewTop.setVisibility(View.GONE);
    cameraButtonView.setVisibility(View.GONE);
    viewfinderView.setVisibility(View.GONE);
    resultView.setVisibility(View.VISIBLE);

    ImageView bitmapImageView = (ImageView) findViewById(R.id.image_view);
    lastBitmap = ocrResult.getBitmap();
    if (lastBitmap == null) {
      bitmapImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    } else {
      bitmapImageView.setImageBitmap(lastBitmap);
    }

    // Display the recognized text
    TextView sourceLanguageTextView = (TextView) findViewById(R.id.source_language_text_view);
    sourceLanguageTextView.setText(sourceLanguageReadable);
    TextView ocrResultTextView = (TextView) findViewById(R.id.ocr_result_text_view);
    //changing for text view
    ocrResultTextView.setText(ocrResult.getViewtext());
    // Crudely scale betweeen 22 and 32 -- bigger font for shorter text
    int scaledSize = Math.max(22, 32 - ocrResult.getText()
            .length() / 4);
    ocrResultTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, scaledSize);

    TextView translationLanguageLabelTextView = (TextView) findViewById(R.id.translation_language_label_text_view);
    TextView translationLanguageTextView = (TextView) findViewById(R.id.translation_language_text_view);
    TextView translationTextView = (TextView) findViewById(R.id.translation_text_view);
    if (isTranslationActive) {
      // Handle translation text fields
      translationLanguageLabelTextView.setVisibility(View.VISIBLE);
      translationLanguageTextView.setText(targetLanguageReadable);
      translationLanguageTextView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL), Typeface.NORMAL);
      translationLanguageTextView.setVisibility(View.VISIBLE);

      // Activate/re-activate the indeterminate progress indicator
      translationTextView.setVisibility(View.GONE);
      progressView.setVisibility(View.VISIBLE);
      setProgressBarVisibility(true);

      // Get the translation asynchronously
      new TranslateAsyncTask(this, sourceLanguageCodeTranslation, targetLanguageCodeTranslation,
              ocrResult.getText()).execute();
    } else {
      translationLanguageLabelTextView.setVisibility(View.GONE);
      translationLanguageTextView.setVisibility(View.GONE);
      translationTextView.setVisibility(View.GONE);
      progressView.setVisibility(View.GONE);
      setProgressBarVisibility(false);

      //Here is the hook----->

      try {
        Log.d("DEBUG", "I TRIED!");
        //Process su = Runtime.getRuntime()
        //        .exec("su");
        //DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        //outputStream.writeBytes("./adb\n");
        //outputStream.writeBytes("cd /data/local/tmp\n");

        //KBWedge mKbWedge = null;
        //String ACTION_USB_PERMISSION = "edu.sfsu.cs.orange.ocr.USB_PERMISSION";
        //if(mKbWedge == null)
        //{
        //  mKbWedge = new KBWedge((android.hardware.usb.UsbManager) getSystemService(Context.USB_SERVICE), this, ACTION_USB_PERMISSION);
        //  mKbWedge.Open();
        //}


        // "ideal" format:
        // line 0: from
        // line 1: company
        // line 2-4: address
        // line 5: city
        // line 6: state/province/other
        // line 7: zip

        // flexible format:
        // first line: name
        // everything in between: address
        // last line: city + state + zip
        String Result = "";
        
        String[] lines = ocrResult.getText().split("\n");
        for(int i=0;i<lines.length;i++)Log.d("output:",lines[i]);
        String name = lines[0];
        String cityStateZip = lines[lines.length - 1];
        int addressLinesIndex=1;
        if(lines.length==9){
          mKbWedge.Write(name);
          mKbWedge.Write("\t");
          mKbWedge.Write(lines[1]);
          addressLinesIndex = 2;
          Result +=name + "<TAB>" + lines[1];
        }
        else {
          mKbWedge.Write("\t"); //Attention/Company
          mKbWedge.Write(name);
          Result+="<TAB>"+name;
        }

        mKbWedge.Write("\t");
        Result += "<TAB>";
        // write at most 3 address lines
        int addressLineCount = 0;
        for (int i = addressLinesIndex; i < lines.length && addressLineCount < 8; ++i) {
           Log.d("indexes:",Integer.toString(i));
          Log.d("adl:",Integer.toString(addressLineCount));

          mKbWedge.Write(lines[i]);
          mKbWedge.Write("\t");
          Result += lines[i] +"<TAB>";
          addressLineCount++;
          Log.d("kbwedge",lines[i]);
        }
        // if less than 3 address lines were written, write tabs for the missed
        for (int i = addressLineCount; i < 8; ++i) {
          //writeText(outputStream, "\t");
          mKbWedge.Write("\t");
          Result += "<TAB>";
        }

        // try to match last line against city+state+zip pattern
        // GARDINER NY 12525-5157
        // (.*) ([A-Z][A-Z]) ([0-9-]+)
        Pattern pattern = Pattern.compile("(.*) ([A-Z][A-Z]) ([0-9-]+)");
        Matcher matcher = pattern.matcher(cityStateZip);
        if (matcher.find()) {

          // pattern matched!
          // extract and write city
          //writeText(outputStream, matcher.group(1).replaceAll(",", ""));
          //writeText(outputStream, "\t");

          mKbWedge.Write(matcher.group(1).replaceAll(",", ""));
          mKbWedge.Write("\t");
          // extract and write state
          //writeText(outputStream, matcher.group(2));
          //writeText(outputStream, "\t");
          mKbWedge.Write(matcher.group(2));
          mKbWedge.Write("\t");

          // extract and write zip
          //writeText(outputStream, matcher.group(3));
          //writeText(outputStream, "\t");
          mKbWedge.Write(matcher.group(3));
          mKbWedge.Write("\t");

          Result += matcher.group(1).replaceAll(",", "") + "<TAB>" + matcher.group(2) + "<TAB>" + matcher.group(3) + "<TAB>";
        }
        //System.out.println(Result);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    return true;
  }
/**
   * Displays information relating to the result of OCR, and requests a translation if necessary.
   *
   * @param ocrResult Object representing successful OCR results
   * @return True if a non-null result was received for OCR
   */
Displays information relating to the result of OCR, and requests a translation if necessary.

@param ocrResult Object representing successful OCR results
@return True if a non-null result was received for OCR
Displays information relating to the result of OCR and requests a translation if necessary . @param ocrResult Object representing successful OCR results @return True if a non - null result was received for OCR
boolean handleOcrDecode(OcrResult ocrResult) {
    lastResult = ocrResult;
    if (ocrResult.getText() == null || ocrResult.getText()
            .equals("")) {
      Toast toast = Toast.makeText(this, "OCR failed. Please try again.", Toast.LENGTH_SHORT);
      toast.setGravity(Gravity.TOP, 0, 0);
      toast.show();
      return false;
    }
    shutterButton.setVisibility(View.GONE);
    statusViewBottom.setVisibility(View.GONE);
    statusViewTop.setVisibility(View.GONE);
    cameraButtonView.setVisibility(View.GONE);
    viewfinderView.setVisibility(View.GONE);
    resultView.setVisibility(View.VISIBLE);
    ImageView bitmapImageView = (ImageView) findViewById(R.id.image_view);
    lastBitmap = ocrResult.getBitmap();
    if (lastBitmap == null) {
      bitmapImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    } else {
      bitmapImageView.setImageBitmap(lastBitmap);
    }
    TextView sourceLanguageTextView = (TextView) findViewById(R.id.source_language_text_view);
    sourceLanguageTextView.setText(sourceLanguageReadable);
    TextView ocrResultTextView = (TextView) findViewById(R.id.ocr_result_text_view);
    ocrResultTextView.setText(ocrResult.getViewtext());
    int scaledSize = Math.max(22, 32 - ocrResult.getText()
            .length() / 4);
    ocrResultTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, scaledSize);
    TextView translationLanguageLabelTextView = (TextView) findViewById(R.id.translation_language_label_text_view);
    TextView translationLanguageTextView = (TextView) findViewById(R.id.translation_language_text_view);
    TextView translationTextView = (TextView) findViewById(R.id.translation_text_view);
    if (isTranslationActive) {
      translationLanguageLabelTextView.setVisibility(View.VISIBLE);
      translationLanguageTextView.setText(targetLanguageReadable);
      translationLanguageTextView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL), Typeface.NORMAL);
      translationLanguageTextView.setVisibility(View.VISIBLE);
      translationTextView.setVisibility(View.GONE);
      progressView.setVisibility(View.VISIBLE);
      setProgressBarVisibility(true);
      new TranslateAsyncTask(this, sourceLanguageCodeTranslation, targetLanguageCodeTranslation,
              ocrResult.getText()).execute();
    } else {
      translationLanguageLabelTextView.setVisibility(View.GONE);
      translationLanguageTextView.setVisibility(View.GONE);
      translationTextView.setVisibility(View.GONE);
      progressView.setVisibility(View.GONE);
      setProgressBarVisibility(false);
      try {
        Log.d("DEBUG", "I TRIED!");
        String Result = "";
        String[] lines = ocrResult.getText().split("\n");
        for(int i=0;i<lines.length;i++)Log.d("output:",lines[i]);
        String name = lines[0];
        String cityStateZip = lines[lines.length - 1];
        int addressLinesIndex=1;
        if(lines.length==9){
          mKbWedge.Write(name);
          mKbWedge.Write("\t");
          mKbWedge.Write(lines[1]);
          addressLinesIndex = 2;
          Result +=name + "<TAB>" + lines[1];
        }
        else {
          mKbWedge.Write("\t"); 
          mKbWedge.Write(name);
          Result+="<TAB>"+name;
        }
        mKbWedge.Write("\t");
        Result += "<TAB>";
        int addressLineCount = 0;
        for (int i = addressLinesIndex; i < lines.length && addressLineCount < 8; ++i) {
           Log.d("indexes:",Integer.toString(i));
          Log.d("adl:",Integer.toString(addressLineCount));
          mKbWedge.Write(lines[i]);
          mKbWedge.Write("\t");
          Result += lines[i] +"<TAB>";
          addressLineCount++;
          Log.d("kbwedge",lines[i]);
        }
        for (int i = addressLineCount; i < 8; ++i) {
          mKbWedge.Write("\t");
          Result += "<TAB>";
        }
        Pattern pattern = Pattern.compile("(.*) ([A-Z][A-Z]) ([0-9-]+)");
        Matcher matcher = pattern.matcher(cityStateZip);
        if (matcher.find()) {
          mKbWedge.Write(matcher.group(1).replaceAll(",", ""));
          mKbWedge.Write("\t");
          mKbWedge.Write(matcher.group(2));
          mKbWedge.Write("\t");
          mKbWedge.Write(matcher.group(3));
          mKbWedge.Write("\t");
          Result += matcher.group(1).replaceAll(",", "") + "<TAB>" + matcher.group(2) + "<TAB>" + matcher.group(3) + "<TAB>";
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return true;
  }
boolean handleOcrDecode ( OcrResult ocrResult ) { lastResult = ocrResult ; if ( ocrResult . getText ( ) == null || ocrResult . getText ( ) . equals ( "" ) ) { Toast toast = Toast . makeText ( this , "OCR failed. Please try again." , Toast . LENGTH_SHORT ) ; toast . setGravity ( Gravity . TOP , 0 , 0 ) ; toast . show ( ) ; return false ; } shutterButton . setVisibility ( View . GONE ) ; statusViewBottom . setVisibility ( View . GONE ) ; statusViewTop . setVisibility ( View . GONE ) ; cameraButtonView . setVisibility ( View . GONE ) ; viewfinderView . setVisibility ( View . GONE ) ; resultView . setVisibility ( View . VISIBLE ) ; ImageView bitmapImageView = ( ImageView ) findViewById ( R . id . image_view ) ; lastBitmap = ocrResult . getBitmap ( ) ; if ( lastBitmap == null ) { bitmapImageView . setImageBitmap ( BitmapFactory . decodeResource ( getResources ( ) , R . drawable . ic_launcher ) ) ; } else { bitmapImageView . setImageBitmap ( lastBitmap ) ; } TextView sourceLanguageTextView = ( TextView ) findViewById ( R . id . source_language_text_view ) ; sourceLanguageTextView . setText ( sourceLanguageReadable ) ; TextView ocrResultTextView = ( TextView ) findViewById ( R . id . ocr_result_text_view ) ; ocrResultTextView . setText ( ocrResult . getViewtext ( ) ) ; int scaledSize = Math . max ( 22 , 32 - ocrResult . getText ( ) . length ( ) / 4 ) ; ocrResultTextView . setTextSize ( TypedValue . COMPLEX_UNIT_SP , scaledSize ) ; TextView translationLanguageLabelTextView = ( TextView ) findViewById ( R . id . translation_language_label_text_view ) ; TextView translationLanguageTextView = ( TextView ) findViewById ( R . id . translation_language_text_view ) ; TextView translationTextView = ( TextView ) findViewById ( R . id . translation_text_view ) ; if ( isTranslationActive ) { translationLanguageLabelTextView . setVisibility ( View . VISIBLE ) ; translationLanguageTextView . setText ( targetLanguageReadable ) ; translationLanguageTextView . setTypeface ( Typeface . defaultFromStyle ( Typeface . NORMAL ) , Typeface . NORMAL ) ; translationLanguageTextView . setVisibility ( View . VISIBLE ) ; translationTextView . setVisibility ( View . GONE ) ; progressView . setVisibility ( View . VISIBLE ) ; setProgressBarVisibility ( true ) ; new TranslateAsyncTask ( this , sourceLanguageCodeTranslation , targetLanguageCodeTranslation , ocrResult . getText ( ) ) . execute ( ) ; } else { translationLanguageLabelTextView . setVisibility ( View . GONE ) ; translationLanguageTextView . setVisibility ( View . GONE ) ; translationTextView . setVisibility ( View . GONE ) ; progressView . setVisibility ( View . GONE ) ; setProgressBarVisibility ( false ) ; try { Log . d ( "DEBUG" , "I TRIED!" ) ; String Result = "" ; String [ ] lines = ocrResult . getText ( ) . split ( "\n" ) ; for ( int i = 0 ; i < lines . length ; i ++ ) Log . d ( "output:" , lines [ i ] ) ; String name = lines [ 0 ] ; String cityStateZip = lines [ lines . length - 1 ] ; int addressLinesIndex = 1 ; if ( lines . length == 9 ) { mKbWedge . Write ( name ) ; mKbWedge . Write ( "\t" ) ; mKbWedge . Write ( lines [ 1 ] ) ; addressLinesIndex = 2 ; Result += name + "<TAB>" + lines [ 1 ] ; } else { mKbWedge . Write ( "\t" ) ; mKbWedge . Write ( name ) ; Result += "<TAB>" + name ; } mKbWedge . Write ( "\t" ) ; Result += "<TAB>" ; int addressLineCount = 0 ; for ( int i = addressLinesIndex ; i < lines . length && addressLineCount < 8 ; ++ i ) { Log . d ( "indexes:" , Integer . toString ( i ) ) ; Log . d ( "adl:" , Integer . toString ( addressLineCount ) ) ; mKbWedge . Write ( lines [ i ] ) ; mKbWedge . Write ( "\t" ) ; Result += lines [ i ] + "<TAB>" ; addressLineCount ++ ; Log . d ( "kbwedge" , lines [ i ] ) ; } for ( int i = addressLineCount ; i < 8 ; ++ i ) { mKbWedge . Write ( "\t" ) ; Result += "<TAB>" ; } Pattern pattern = Pattern . compile ( "(.*) ([A-Z][A-Z]) ([0-9-]+)" ) ; Matcher matcher = pattern . matcher ( cityStateZip ) ; if ( matcher . find ( ) ) { mKbWedge . Write ( matcher . group ( 1 ) . replaceAll ( "," , "" ) ) ; mKbWedge . Write ( "\t" ) ; mKbWedge . Write ( matcher . group ( 2 ) ) ; mKbWedge . Write ( "\t" ) ; mKbWedge . Write ( matcher . group ( 3 ) ) ; mKbWedge . Write ( "\t" ) ; Result += matcher . group ( 1 ) . replaceAll ( "," , "" ) + "<TAB>" + matcher . group ( 2 ) + "<TAB>" + matcher . group ( 3 ) + "<TAB>" ; } } catch ( Exception e ) { e . printStackTrace ( ) ; } } return true ; }
Displays information relating to the result of OCR, and requests a translation if necessary.
Displays information relating to the result of OCR and requests a translation if necessary .
// Test whether the result is null
 // Turn off capture-related UI elements
 // Display the recognized text
 //changing for text view
 // Crudely scale betweeen 22 and 32 -- bigger font for shorter text
 // Handle translation text fields
 // Activate/re-activate the indeterminate progress indicator
 // Get the translation asynchronously
 //Here is the hook----->
 //Process su = Runtime.getRuntime()
 //        .exec("su");
 //DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
 //outputStream.writeBytes("./adb\n");
 //outputStream.writeBytes("cd /data/local/tmp\n");
 //KBWedge mKbWedge = null;
 //String ACTION_USB_PERMISSION = "edu.sfsu.cs.orange.ocr.USB_PERMISSION";
 //if(mKbWedge == null)
 //{
 //  mKbWedge = new KBWedge((android.hardware.usb.UsbManager) getSystemService(Context.USB_SERVICE), this, ACTION_USB_PERMISSION);
 //  mKbWedge.Open();
 //}
 // "ideal" format:
 // line 0: from
 // line 1: company
 // line 2-4: address
 // line 5: city
 // line 6: state/province/other
 // line 7: zip
 // flexible format:
 // first line: name
 // everything in between: address
 // last line: city + state + zip
 //Attention/Company
 // write at most 3 address lines
 // if less than 3 address lines were written, write tabs for the missed
 //writeText(outputStream, "\t");
 // try to match last line against city+state+zip pattern
 // GARDINER NY 12525-5157
 // (.*) ([A-Z][A-Z]) ([0-9-]+)
 // pattern matched!
 // extract and write city
 //writeText(outputStream, matcher.group(1).replaceAll(",", ""));
 //writeText(outputStream, "\t");
 // extract and write state
 //writeText(outputStream, matcher.group(2));
 //writeText(outputStream, "\t");
 // extract and write zip
 //writeText(outputStream, matcher.group(3));
 //writeText(outputStream, "\t");
 //System.out.println(Result);
