Camera on Android Example

37,954

Solution 1

With approach 2 the issues is stability. I tried some examples, but I've managed to stop the camera from working (until a restart) on some devices and completely freeze another device. On another device the capture worked, but the preview stayed black.

Either there is a bug in the examples or there is a compatibility issue with the devices.

Solution 2

The example that CommonsWare gave works well. The example works when using it as-is, but here are the issues I ran into when modifying it for my use case:

  1. Never take a second picture before the first picture has completed, in other words PictureCallback.onPictureTaken() has been called. The CommonsWare example uses the inPreview flag for this purpose.
  2. Make sure that your SurfaceView is full-screen. If you want a smaller preview you might need to change the preview size selection logic, otherwise the preview might not fit into the SurfaceView on some devices. Some devices only support a full-screen preview size, so keeping it full-screen is the simplest solution.

To add more components to the preview screen, FrameLayout works well in my experience. I started by using a LinearLayout to add text above the preview, but that broke rule #2. When using a FrameLayout to add components on top of the preview, you don't have any issues with the preview resolution.

I also posted a minor issue relating to Camera.open() on GitHub.

Share:
37,954
Ralf
Author by

Ralf

Updated on October 02, 2020

Comments

  • Ralf
    Ralf over 3 years

    I want to write an activity that:

    1. Shows the camera preview (viewfinder), and has a "capture" button.
    2. When the "capture" button is pressed, takes a picture and returns it to the calling activity (setResult() & finish()).

    Are there any complete examples out there that works on every device? A link to a simple open source application that takes pictures would be the ideal answer.


    My research so far:

    This is a common scenario, and there are many questions and tutorials on this.

    There are two main approaches:

    1. Use the android.provider.MediaStore.ACTION_IMAGE_CAPTURE event. See this question
    2. Use the Camera API directly. See this example or this question (with lots of references).

    Approach 1 would have been perfect, but the issue is that the intent is implemented differently on each device. On some devices it works well. However, on some devices you can take a picture but it is never returned to your app. On some devices nothing happens when you launch the intent. Typically it also saves the picture to the SD card, and requires the SD card to be present. The user interaction is also different on every device.

    With approach 2 the issues is stability. I tried some examples, but I've managed to stop the camera from working (until a restart) on some devices and completely freeze another device. On another device the capture worked, but the preview stayed black.

    I would have used ZXing as an example application (I work with it a lot), but it only uses the preview (viewfinder), and doesn't take any pictures. I also found that on some devices, ZXing did not automatically adjust the white balance when the lighting conditions changed, while the native camera app did it properly (not sure if this can be fixed).


    Update:

    For a while I used the camera API directly. This gives more control (custom UI, etc), but I would not recommend it to anyone. I would work on 90% of devices, but every now and again a new device would be released, with a different problem.

    Some of the problems I've encountered:

    • Handling autofocus
    • Handling flash
    • Supporting devices with a front camera, back camera or both
    • Each device has a different combination of screen resolution, preview resolutions (doesn't always match the screen resolution) and picture resolutions.

    So in general, I'd not recommend going this route at all, unless there is no other way. After two years I dumped by custom code and switched back to the Intent-based approach. Since then I've had much less trouble. The issues I've had with the Intent-based approach in the past was probably just my own incompetence.

    If you really need to go this route, I've heard it's much easier if you only support devices with Android 4.0+.

  • CommonsWare
    CommonsWare over 12 years
    Your point #2 is incorrect. I am using the size that is supplied by the SurfaceView itself, which should be valid for any size SurfaceView. There is no assumption that it is full-screen -- in fact, on Android 3.0+, it is not full-screen, as there is an action bar.
  • Ralf
    Ralf over 12 years
    The issue I had was that on one device, the only preview size is 320x240 (full-screen resolution). If the SurfaceView is smaller than that, the example will not display any preview.
  • CommonsWare
    CommonsWare over 12 years
    Out of curiosity, which device? There's no question that my code assumes that there is at least one valid preview resolution smaller than the SurfaceView size.
  • Ralf
    Ralf over 12 years
    It's the Samsung Galaxy Y Pro B5510 (good entry-level phone). On all the other phones I tested this wasn't an issue. I edited the answer to improve the wording of point #2.
  • AgentKnopf
    AgentKnopf about 12 years
    I tried your example and I noticed one thing: When I open the preview, then send my device to sleep (the button on the right side of the Samsung Galaxy SII) and wake it up again, the Preview Screen is all black and taking pictures fails. I imagine it must've sth to do with the onPause/onResume implementation?
  • CommonsWare
    CommonsWare about 12 years
    @Zainodis: Make sure that you are on the latest version of the sample -- I updated it a month or so ago based on some feedback regarding this issue.
  • AgentKnopf
    AgentKnopf about 12 years
    Ah thanks, I just implemented it 2 days ago but I'll recheck :) I'll let u know if I still run into this issue after updating to the latest version of your code.
  • AgentKnopf
    AgentKnopf about 12 years
    Ok I made sure now my code matches this one: github.com/commonsguy/cw-advandroid/blob/master/Camera/Pictu‌​re/… however the issue at hand persists - any idea how to trouble-shoot this? I am running your sample on a Samsung Galaxy SII with Android Version 2.3.4. On an additional note: I am starting your camera example using startActvityForResult - though I am not sure this matter for the issue I am having with sleep.
  • CommonsWare
    CommonsWare about 12 years
    @Zainodis: "any idea how to trouble-shoot this?" -- no I don't, sorry. Can you give me the exact Samsung model of your device (Settings > About phone)?
  • AgentKnopf
    AgentKnopf about 12 years
    The Model is GT-I9100. Some additional information: Build Number: Gingerbread.XWKI4, Kernel Version: 2.6.35.7-I9100XWKI4-CL575468 and Android Version: 2.3.4 (I think there is an update out already which updates the Phone to 4.0.4 - but I need to reproduce some issues on 2.3.x so I can't udpate just yet).
  • CommonsWare
    CommonsWare about 12 years
    @Zainodis: OK, I will see if I can get my hands on one of these. Meanwhile, I have opened an issue on this -- please use that issue for any more information you uncover. Thanks!
  • AgentKnopf
    AgentKnopf about 12 years
    Thank you ! If you need help testing sth I could help - I'll stick with the 2.3.4 version for a while. And If I come across additional info I'll post it in the issue u opened.
  • C--
    C-- over 11 years
    @CommonsWare This code does not work for me either. One problem is that the camera view finder returns a skewed view of the original object I try to shoot. Second issue is that the picture take button does nothing but receives my click. Tried only JellyBean here. Thanks.
  • Akanksha Rathore
    Akanksha Rathore over 10 years
    Its working only with front camera. what about back camera implementation for the same?
  • Zala Janaksinh
    Zala Janaksinh over 10 years
    @CommonsWare in Micromax HD back camera not working properly,Capture image is alright but in this device I show the blank screen,Not show the preview image like other device,But front camera will work fine.
  • anand
    anand over 9 years
    @CommonsWare I used your camera application it's working fine too ..But when i am capturing the images the camera orientation is set to horizontal view instead of vertical view