从Android应用程序活动开发Android库

时间:2017-01-25 13:23:20

标签: android interface sdk android-camera tesseract

我有一个摄像机活动,它会对图像进行OCR并将结果作为意图返回给调用它的活动。

现在,我想将它设为 ,以便可以通过简单的方式调用它,例如 Scanner.readText() 命令。我希望通过简单的Scanner.readText()命令打开相机活动,扫描图像,返回OCR结果并返回原始活动。

我该如何处理?我的SDK界面应该如何?如何实现回调?

我的代码的要点

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    rl = (RelativeLayout) findViewById(R.id.rel_camera);
    iv = (ImageView) findViewById(R.id.black_above);

    tessOCR = new TessOCR(getApplicationContext());

}

@Override
protected void onStart() {
    super.onStart();
    // Create an instance of Camera
    mCamera = getCameraInstance();

    mPreview = new CameraPreview(this, mCamera);
    preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    rl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);
        }
    });

}


@Override
protected void onDestroy() {
    super.onDestroy();
    tessOCR.onDestroy();
}



private class MRZ_OCR extends AsyncTask<Void, Void, String>

{
    private byte[] data;
    private String result;

    public MRZ_OCR(byte[] data) {
        this.data = data;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        CameraActivity.this.pd = ProgressDialog.show(CameraActivity.this, null, "Doing OCR");
    }

    @Override
    protected String doInBackground(Void... params) {


        // Crop to get only MRZ
        Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
        bm = Bitmap.createBitmap(bm, 0, pxFromDp(CameraActivity.this, 120), viewWidth, viewHeight);

        //  Verify if it has MRZ
        //  bm = MRZ.getMRZ(bm);

        if (bm != null) {

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            createImageFile(byteArray);
        }


        //binarize the image

        for (int i = 0; i < bm.getWidth(); i++) {
            for (int c = 0; c < bm.getHeight(); c++) {
                int pixel = bm.getPixel(i, c);
                if (shouldBeBlack(pixel))
                    bm.setPixel(i, c, Color.BLACK);
                else
                    bm.setPixel(i, c, Color.WHITE);
            }
        }


        //create image file
        if (bm != null) {

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            createImageFile(byteArray);
        }

        result = tessOCR.getOCRResult(bm);

        return result;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Intent intent = new Intent(CameraActivity.this, MainActivity.class);
        intent.putExtra("MRZ", s);
        startActivity(intent);

        pd.dismiss();


    }
} 

0 个答案:

没有答案