OpenCV + Tesseract on Android

最近不务正业,决定做一个Android的小应用。
想起以前还曾经发誓,坚决不碰Java和移动开发。。。哎

有两个主要难点(对我来说):

  • OpenCV + Tesseract on Android
  • 摄像头实时处理,包括读取和显示(read and display)

OpenCV + Tesseract on Android

Real time processing of Android camera

Android Camera API - Tutorial

- Using the camera on the Android device can be done via the integration of existing camera application. In this case you would start the existing Camera application via an intent and use the return data of the application to access the result.

- Alternatively you can also directly integrate the camera into your application via the Camera API.
Access camera by Intent
  • Read from gallery (ACTION_PICK and ACTION_GET_CONTENT)

How to pick an image from gallery (SD Card) for my app?

With ACTION_PICK you specify a specific URI and with ACTION_GET_CONTENT you specify a mime_type.

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream = getContentResolver().openInputStream(selectedImage);
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
        }
    }
}

and Resize

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

// The new size we want to scale to
final int REQUIRED_SIZE = 140;

// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
               || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);

}
  • Read from camera

Intent to choose between the camera or the gallery in Android

Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
startActivityForResult(intent, PHOTOALBUM);
  • Read from camera
Intent cameraIntent = new Intent(            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PICTURE);
.....
 if (requestcode == TAKE_PICTURE) {
picUri = intent.getData();
MediaStore.Images.Media.getBitmap(getContentResolver(), picUri);
  • Read from camera and save
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment .getExternalStorageDirectory(), "MyImage.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bitmap mBitmap;
        if (requestCode == REQUEST_CAMERA) {
            File camFile = new File(Environment.getExternalStorageDirectory()
                    .toString());
            for (File temp : camFile.listFiles()) {
                if (temp.getName().equals("MyImage.jpg")) {
                    camFile = temp;
                    break;
                }
            }
            try {

                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();

                mBitmap = BitmapFactory.decodeFile(camFile.getAbsolutePath(),
                        btmapOptions);
                //Here you have the bitmap of the image from camera

            } catch (Exception e) {
                e.printStackTrace();
            }
  • Do Both
Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT, null);
galleryintent.setType("image/*");

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryintent);
chooser.putExtra(Intent.EXTRA_TITLE, "Select from:");

Intent[] intentArray = { cameraIntent };    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser, REQUEST_PIC);
Access camera by API
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值