简体   繁体   English

使用 OpenCV 获取 jpg 文件的路径以在 Android Studio 应用程序中制作 Mat

[英]Getting the path of a jpg file to make a Mat in Android Studio app using OpenCV

I'm an absolute beginner currently making an app in Android Studio.我是一个绝对的初学者,目前正在 Android Studio 中制作应用程序。 I want it to be able to take a photo of a barcode, save it, do some image processing on it with OpenCV and then decode it with Zxing.我希望它能够拍摄条形码的照片,保存它,使用 OpenCV 对其进行一些图像处理,然后使用 Zxing 对其进行解码。

Here's the code of the activity that's supposed to do all of that (so far):这是应该完成所有这些(到目前为止)的活动的代码:

public class Scan extends AppCompatActivity {

    private Uri file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        openCamera();
    }

    public void openCamera(){
        Intent intent_Camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        file = Uri.fromFile(getOutputMediaFile());
        intent_Camera.putExtra(MediaStore.EXTRA_OUTPUT, file);
        startActivityForResult(intent_Camera, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100) {
            if (resultCode == RESULT_CANCELED) {
                finish();
            }
        }
    }

    private static File getOutputMediaFile(){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyApp");

        if (!mediaStorageDir.exists()){
            if (!mediaStorageDir.mkdirs()){
                return null;
            }
        }

        return new File(mediaStorageDir.getPath() + File.separator +
                "TEMP" + ".jpg");                                                        
    }

Next I want to add a method that will create a Mat out of the jpg file and then do some other things, so far it looks like this:接下来我想添加一个方法,该方法将从 jpg 文件中创建一个 Mat 然后做一些其他的事情,到目前为止它看起来像这样:

public static String decodePic(){
        Imgcodecs imageCodecs = new Imgcodecs();
        String file ="???";
        Mat matPic = imageCodecs.imread(file);

I have no idea how to get the path of my jpg in a "smart" way though.我不知道如何以“智能”的方式获得我的 jpg 的路径。 I could insert the actual path of it the way it's on my phone in the place of the ???'s but I'm pretty sure it can be different for different phones.我可以将它的实际路径插入到我手机上的 ??? 位置,但我很确定不同的手机可能会有所不同。 I thought about changing it a bit and doing something similar to the way it's done with the mediaStorageDir File, but I have no idea how.我想稍微改变它并做一些类似于使用 mediaStorageDir 文件的方式,但我不知道如何做。 Is there an easy way to do it?有没有简单的方法来做到这一点?

From your getOutputMediaFile() method take file's object and use command above.从您的 getOutputMediaFile() 方法获取文件的对象并使用上面的命令。

file.getAbsolutePath() file.getAbsolutePath()

Pass this return in your imread method在您的 imread 方法中传递此返回值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM