I'm an absolute beginner currently making an app in 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.
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:
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. 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. Is there an easy way to do it?
From your getOutputMediaFile() method take file's object and use command above.
file.getAbsolutePath()
Pass this return in your imread method