如何捕获Mat对象并将其传递给新活动?

时间:2018-08-15 16:02:07

标签: java android opencv opencv4android

我正在计划实施ocr,其目的是让用户捕获图像,然后应用程序会将其作为文本写入手机中的文件。我已经设法使用CvCameraViewListener2从摄像机显示到javacameraview的提要。

我现在想将框架捕获为Mat对象并将其传递给新的活动。该解决方案的我所看到的全部涉及保存照片,显示处理后的图像或实时处理。我只需要将Mat传递给另一个类进行处理,然后就可以释放它。我似乎很难找到一种方法来执行此操作,尽管看起来应该很简单。

问题本质上是如何从cameraview中获取Mat对象?

任何帮助或指导将不胜感激。

谢谢stackoverflow

相关代码在这里

import android.Manifest;
import android.content.Intent;
Import android.content.pm.ActivityInfo; importandroid.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Import android.widget.Toast;


        public class MainActivity extends AppCompatActivity {
            private static final int REQUEST_CAMERA_PERMISSION_RESULT = 1;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                if (getResources().getConfiguration().orientation != 90) {
                    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
                }
                setContentView(R.layout.activity_main);

                Button button = (Button) findViewById(R.id.cameraInit);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //marshmallow permissions
                        takePhoto();

                    }
                });
            }

            private void callCamera(){
                Intent startCameraIntent = new Intent(getApplicationContext(), OpenCVCamera.class);
                startActivity(startCameraIntent);
            }

            public void takePhoto() {
                if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                    if(ContextCompat.checkSelfPermission(this,
                            Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
                        callCamera();
                    } else {
                        if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){
                            Toast.makeText(this, "This app requires camera access", Toast.LENGTH_SHORT).show();
                        }

                        requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION_RESULT);
                    }
                } else {
                    callCamera();

                }
            }

            @Override
            public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

                if(requestCode == REQUEST_CAMERA_PERMISSION_RESULT){
                    if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                        callCamera();
                    } else {
                        Toast.makeText(this, "Permission Denied.", Toast.LENGTH_SHORT).show();

                    }
                } else {
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                }

            }
        }


    import android.content.pm.ActivityInfo;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import android.util.Log;

        import android.view.SurfaceView;
        import android.view.WindowManager;

            import org.opencv.android.BaseLoaderCallback;
            import org.opencv.android.CameraBridgeViewBase;
            import org.opencv.android.LoaderCallbackInterface;
            import org.opencv.android.OpenCVLoader;
            import org.opencv.core.Core;
            import org.opencv.core.Mat;

        public class OpenCVCamera extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{
        // need to correct view size (i think this can be done in the xml
        // else refer to cmaera video app
        // also orientation needs to be corrected

            private static final String TAG = "OpenCVCamera";
            private CameraBridgeViewBase cameraBridgeViewBase;

            private BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback(this) {
                @Override
                public void onManagerConnected(int status) {
                    switch (status) {
                        case LoaderCallbackInterface.SUCCESS:
                            cameraBridgeViewBase.enableView();
                            break;
                        default:
                            super.onManagerConnected(status);
                            break;
                    }
                }
            };
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                setContentView(R.layout.activity_open_cvcamera);
                cameraBridgeViewBase = (CameraBridgeViewBase) findViewById(R.id.camera_view);
                cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
                cameraBridgeViewBase.setCvCameraViewListener(this);
            }

            @Override
            public void onResume(){
                super.onResume();
                if (!OpenCVLoader.initDebug()) {
                    Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
                    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, baseLoaderCallback);
                } else {
                    Log.d(TAG, "OpenCV library found inside package. Using it!");
                    baseLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
                }
            }
            @Override
            public void onCameraViewStarted(int width, int height) {

            }

            @Override
            public void onCameraViewStopped() {

            }

            @Override
            public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
                //original just returns inputFrame.rgba()
                Core.rotate(inputFrame.rgba(), inputFrame.rgba(), Core.ROTATE_90_CLOCKWISE); //ROTATE_180 or ROTATE_90_COUNTERCLOCKWISE
                return inputFrame.rgba();
            }
        }

0 个答案:

没有答案