2017-06-23 6 views
0

Also, ich entwickle OCR-Anwendung mit offensichtlichen Bild zu Text-Funktionalität in Android Studio. Ich halte mich selbst in der Programmierung für noob. Im Moment folge ich this Tutorial und ich bin ziemlich zufrieden mit den Ergebnissen gegen andere Tutorials, denen ich folgte. Allerdings gibt es dieses eine Problem, das ich der App hinzufügen wollte, entweder während der Kamera oder nach dem Bild. Die beste Option wäre es, einen Rect-Bildschirm auf dem Bildschirm zu zeichnen, bei dem der Benutzer die Dimension entsprechend dem benötigten Text ändern kann. Ich möchte jedoch nichts im Code ändern, da es perfekt funktioniert. Kann jemand helfen? HierWie fügt man der OCR-Anwendung eine Crop-Funktionalität hinzu?

ist die MainActivity.java

public class MainActivity extends AppCompatActivity { 
    private static final String LOG_TAG = "Text API"; 
    private static final int PHOTO_REQUEST = 10; 
    private TextView scanResults; 
    private Uri imageUri; 
    private TextRecognizer detector; 
    private static final int REQUEST_WRITE_PERMISSION = 20; 
    private static final String SAVED_INSTANCE_URI = "uri"; 
    private static final String SAVED_INSTANCE_RESULT = "result"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button button = (Button) findViewById(R.id.button); 
     scanResults = (TextView) findViewById(R.id.results); 
     if (savedInstanceState != null) { 
      imageUri = Uri.parse(savedInstanceState.getString(SAVED_INSTANCE_URI)); 
      scanResults.setText(savedInstanceState.getString(SAVED_INSTANCE_RESULT)); 
     } 
     detector = new TextRecognizer.Builder(getApplicationContext()).build(); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ActivityCompat.requestPermissions(MainActivity.this, new 
         String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION); 
      } 
     }); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     switch (requestCode) { 
      case REQUEST_WRITE_PERMISSION: 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        takePicture(); 
       } else { 
        Toast.makeText(MainActivity.this, "Permission Denied!", Toast.LENGTH_SHORT).show(); 
       } 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == PHOTO_REQUEST && resultCode == RESULT_OK) { 
      launchMediaScanIntent(); 
      try { 
       Bitmap bitmap = decodeBitmapUri(this, imageUri); 
       if (detector.isOperational() && bitmap != null) { 
        Frame frame = new Frame.Builder().setBitmap(bitmap).build(); 
        SparseArray<TextBlock> textBlocks = detector.detect(frame); 
        String blocks = ""; 
        String lines = ""; 
        String words = ""; 
        for (int index = 0; index < textBlocks.size(); index++) { 
         //extract scanned text blocks here 
         TextBlock tBlock = textBlocks.valueAt(index); 
         blocks = blocks + tBlock.getValue() + "\n" + "\n"; 
         for (Text line : tBlock.getComponents()) { 
          //extract scanned text lines here 
          lines = lines + line.getValue() + "\n"; 
          for (Text element : line.getComponents()) { 
           //extract scanned text words here 
           words = words + element.getValue() + ", "; 
          } 
         } 
        } 
        if (textBlocks.size() == 0) { 
         scanResults.setText("Scan Failed: Found nothing to scan"); 
        } else { 
         scanResults.setText(scanResults.getText() + "Blocks: " + "\n"); 
         scanResults.setText(scanResults.getText() + blocks + "\n"); 
         scanResults.setText(scanResults.getText() + "---------" + "\n"); 
         scanResults.setText(scanResults.getText() + "Lines: " + "\n"); 
         scanResults.setText(scanResults.getText() + lines + "\n"); 
         scanResults.setText(scanResults.getText() + "---------" + "\n"); 
         scanResults.setText(scanResults.getText() + "Words: " + "\n"); 
         scanResults.setText(scanResults.getText() + words + "\n"); 
         scanResults.setText(scanResults.getText() + "---------" + "\n"); 
        } 
       } else { 
        scanResults.setText("Could not set up the detector!"); 
       } 
      } catch (Exception e) { 
       Toast.makeText(this, "Failed to load Image", Toast.LENGTH_SHORT) 
         .show(); 
       Log.e(LOG_TAG, e.toString()); 
      } 
     } 
    } 

    private void takePicture() { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg"); 
     imageUri = Uri.fromFile(photo); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
     startActivityForResult(intent, PHOTO_REQUEST); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     if (imageUri != null) { 
      outState.putString(SAVED_INSTANCE_URI, imageUri.toString()); 
      outState.putString(SAVED_INSTANCE_RESULT, scanResults.getText().toString()); 
     } 
     super.onSaveInstanceState(outState); 
    } 

    private void launchMediaScanIntent() { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     mediaScanIntent.setData(imageUri); 
     this.sendBroadcast(mediaScanIntent); 
    } 

    private Bitmap decodeBitmapUri(Context ctx, Uri uri) throws FileNotFoundException { 
     int targetW = 600; 
     int targetH = 600; 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, bmOptions); 
     int photoW = bmOptions.outWidth; 
     int photoH = bmOptions.outHeight; 

     int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 
     bmOptions.inJustDecodeBounds = false; 
     bmOptions.inSampleSize = scaleFactor; 

     return BitmapFactory.decodeStream(ctx.getContentResolver() 
       .openInputStream(uri), null, bmOptions); 
    } 
} 

Antwort

Verwandte Themen