Getting data back from camera app intent

IronSharpensIron

New Member
Licensed User
Longtime User
Still trying to wrap my head around how B4A handles intents. What I am looking to do is create an intent to launch the camera application:

Dim camintent as Intent
camintent.Initialize("android.media.action.IMAGE_CAPTURE","")
StartActivity(camintent)

works just fine, but what I need is to get the image from that transaction. Either filename (better for me), or image data.

Thanks,
-Sean
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

IronSharpensIron

New Member
Licensed User
Longtime User
Thats cool, my initial version users the advanced camera library. The only problem is I am trying to make a scanning application. When I use the inbuilt cam app it focuses really nice when you take the pic. With the library it doesnt. I have tried all autofocus settings avail. in the library. Is there a way to have it autofocus when you take the pic like the inbuilt camera app does?

Thanks for your reply.
 
Upvote 0

pawelmic

Member
Licensed User
Longtime User
Camera Intent Example, anyone??

Can anybody post a functional Camera Intent Example written in B4A? :sign0163:

- I need to call a Camera app via intent, ie:

B4X:
Dim camintent As Intent
camintent.Initialize("android.media.action.IMAGE_CAPTURE","")
StartActivity(camintent)

- and then be able to retrieve that picture in my app either as data or a file path

I do love the Advanced Camera Library however for my purposes it's an overkill and the only way to get a small resolution image from it is to re-size it "manually" after taking the picture because no matter what you try, the following:

B4X:
Sub Camera1_PictureTaken (Data() As Byte)

will always return the MAX resolution possible on your camera (in my case 5Mpix).

Re-sizing it from 3MB to under 100KB takes too long and I can't have that in my app. Sometimes it takes longer then usual and the phone starts vibrating and pops up a message that application froze (=bad)

I just want to switch quickly to the existing Camera app via intent and grab that tiny picture it produces: 192x256 (or whatever), I don't care if it's of low quality or not.

Any help here would be greatly appreciated.:sign0163:
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
You have to be able to start the Intent with Data Coming Back which can only be done using a library right now. I'm in the same boat wanting better camera functionality. I do like the Capture interface which seems to be consistent on all devices, and I don't think the small image bug exists anymore, so it would be nice to have it. I'm playing with libraries now and trying to either rewrite the Camera Library from B4A or make a working Capture Library...whichever works best. Hopefully I will have something soon.
 
Upvote 0

pawelmic

Member
Licensed User
Longtime User
Can someone translate this to B4A?

(from Android Developer Network)
The following is all in written in JAVA but I'm having a hard time converting it to B4A, perhaps someone more knowledgeable could handle this challenge? ;)

The following example demonstrates how to construct an image capture intent and execute it: Creating Camera Intents

B4X:
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

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

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

and here they show how to receive it after the picture was taken: Receiving Camera Intent

B4X:
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the video capture
        } else {
            // Video capture failed, advise user
        }
    }
}

so we know it can be done but I do better in VB then JAVA :sign0104:
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
If you want to snap a picture with the native camera and save it, you can use this code:
B4X:
Sub OpenCam(Directory As String, PictureName As String)

    Dim i As Intent

    i.Initialize("android.media.action.IMAGE_CAPTURE", "")
    i.PutExtra("output", ParseUri("file://" & File.Combine(Directory, PictureName)))
            
    StartActivity(i)
            
End Sub

Sub ParseUri(FileName As String) As Object

    Dim r As Reflector

    Return r.RunStaticMethod("android.net.Uri", "parse", Array As Object(FileName), Array As String("java.lang.String"))

End Sub

To use it, do something like this:
B4X:
OpenCam(File.DirDefaultexternal, "MyPicture.jpg")
 
Upvote 0

pawelmic

Member
Licensed User
Longtime User
almost ... but not quite ...

Thanks for trying to help! :sign0188:

This is what is happening currently:

- different version of emulators crash after about 2, 3 seconds of emulating the camera view; any idea why? it's not a big deal but can't really test this in the emulator if it crashes

- on a positive side when I try it on my old Samsung Galaxy S phone the camera kicks in fine, I take a picture, I keep it, the camera quits, all great except the picture gets saved into:

/mnt/sdcard/DCIM/Camera/2012-11-02 13.58.38.jpg (random file name!)

and not to the path passed to
B4X:
OpenCam(File.DirRootExternal, "test.jpg")
 
Upvote 0

pawelmic

Member
Licensed User
Longtime User
emulator 2.2 is broken for camera intent

emulator 2.2 is broken.

Workaround: Create an AVD with 2.1 as the target, it works fine!
 
Upvote 0

pawelmic

Member
Licensed User
Longtime User
phone / emulator specific?

the odd (irregular) behavior may be related to a specific phone / emulator :

- Android 2.2 emulator crashes
- Samsung Galaxy S phone saves pictures to the default camera folder with a random file name, and
- Android 2.1 emulator works fine and the camera saves the picture to the name and location that it is told to! :sign0148:
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Does this work any better?

http://www.b4x.com/forum/additional-libraries-classes-official-updates/22931-ezcamera.html

On my end, if the Event comes back True the File is where I told it to be. I tried making the event return the filename it saved to, but the intent returns null...contrary to what Android Documents indicate. I discovered as long as the result is good from the intent then the file is saved where specified. Spent half the day trying to figure out why it wasn't returning anything in intent.getData() like the examples showed...
 
Upvote 0
Top