If I run this demo on the HTC Hero (CyanogenMod 6.1.0) I get a RuntimeException from the Camera.startPreview() method.
This seems to be the same problem described here:
Android Camera will not work. startPreview fails
In other words, you need to switch the width and height around when setting the preview size. Indeed this works but would then break the demo on other devices.
Now, I understand the purpose of the demo is to show how to get a Camera preview up and running (and so this problem is beyond its scope) but I'm wondering if there is a clean workaround for this since I don't want to put "if HTC_HERO" style logic in my code (anyway, I'm sure other devices will have similar behaviour)?
One hack might be to catch the exception and then re-invoke the setPreviewSize() method with swapped params, but I'm hoping to find a nicer way.
I don't want to put "if HTC_HERO" style logic in my code (anyway, I'm sure other devices will have similar behaviour)?
To some extent, that is inevitable, if you are trying to reach 100% of devices running 100% of arbitrary ROM mods, because bugs will abound.
One hack might be to catch the exception and then re-invoke the setPreviewSize() method with swapped params, but I'm hoping to find a nicer way.
In theory, there should be some universal preview size code that determines the right size and configures the SurfaceView accordingly. If the code of the AOSP camera app is any indication, this code will be massive, and it's still unlikely to work everywhere (e.g., off-spec devices or ROM mods violating whatever assumptions that the authors of the "universal" solution considered).
You may wish to examine projects like ZXing's Barcode Scanner and see how they approach the problem.
Related
I was looking for a method to use the camera on android devices without a surfaceview or a preview. I found out that, it is impossible to take picture without that preview. However, I have found a tutorial which is actually working taking pictures without a preview. Here is the link: http://www.vogella.com/articles/AndroidCamera/article.html
After switching the camera in the code from front to back-facing the app didn't crashed but it gave me an error 100. So it is only working with the front cam at the moment.
I am using a Samsung Galaxy S3(4.1.2) and i will test it on a Galaxy S2 and a Galaxy S3 Mini.
Anyone a good explanation for this?
You cannot take a picture without starting preview.
While some Android devices are more flexible, and allow takePicture to be called without preview running, this is technically against the API specifications.
It won't work on a large number of devices, so please don't rely on it. That tutorial is wrong, and presumably tested only on one of the devices that allows this behavior.
If you don't want a visible preview, see this question for ways to do that in Android versions >= 3.0.
Actually the time interval of question and answer is large, but may help others.
You can try this library to take picture even from service:
https://github.com/kevalpatel2106/android-hidden-camera
It uses a feature to draw over other apps and create a fake surface. Hope it helps.
Following the instructions from Google here exactly as it is (QUALITY_HIGH):
http://developer.android.com/guide/topics/media/camera.html#custom-camera
When doing this with a Galaxy S3 (US Version) everything seems fine in indoor lighting. But when the camera goes outside and it is bright (maybe it needs to increase the shutter speed) something strange happens. The video starts "rolling" like a bad TV signal, and the image gets to be very low quality. It almost seems like the image sensor got overloaded and messed up.
I tried recording with the normal camera application and it seems to have no problems under the same condition. But using the API as described here generates this problem. Since the S3 is pretty popular -- anyone run into this problem before?
Is there some kind of hidden setting that the main camera app uses for camera setup? I tried flattening the camera settings to take a look at what's in there and there are tons of settings but i dont know what they all do without documentation. already tried turning off anti-banding and luma-adaptation and that didnt seem to do anything.
Thanks!
I think I figured it out. Need more testing but this seems to do it. There is a hidden setting called "camera-mode" that is normally set to -1. I changed it to a 1 and suddenly it is fine and functioning like the normal camera app.. anyone know what this mode thing actually means?
Camera.Parameters lParam = prCamera.getParameters();
lParam.set("camera-mode",1);
prCamera.setParameters(lParam);
Is what did the trick if anyone else seems to run into the problem.\
I'm trying to develop an app for Android, and I would need to get uncompressed pictures with a resolution as high as possible from the camera. I tried takePicture's rawCallback and postviewCallback, but they are not working.
Right now I'm trying with OpenCV (version 2.4) using VideoCapture, but I'm stuck in the default 960x720, which is poor for what I need; and my phone, a Samsung Galaxy S3, is able to provide, theoretically, up to 8Mpx (3,264×2,448 for pictures, and 1,920×1,080 for video, according to Wikipedia). VideoCapture.set(Highgui.CV_CAP_PROP_FRAME_WIDTH/HEIGHT, some number) makes the camera return a black image as far as I've found.
Is there any way to obtain a higher resolution, either through OpenCV or with the Android API, without compressing?
I'm really sorry if this has been asked before; I have been looking for days and I have found nothing.
Thank you for your time!
EDIT: Although it is not exactly what I was asking, I found that there is a way to do something very similar: if you set an OnPreviewCallback for the Camera, using setPreviewCallback, you do get the raw picture data from the camera (at least in the S3 I'm working with). I leave it here in case somebody finds it useful in the future.
EDIT: A partial solution is explained in an answer below. To sum up,
vc.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, desiredFrameWidth);
vc.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, desiredFrameHeight);
works under some conditions; please see below for further detail.
You have to get supported camera preview resoultions by calling getSupportedPreviewSizes.
After this you can set any resolution with method setPreviewSize. And don't forget to setParameters in the end. Actally many OpenCV Android examples contain this information (look at sample3).
In case anybody ever finds this useful, I found a (partial) solution: If your VideoCapture variable is called vc, this should work:
vc.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, desiredFrameWidth);
vc.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, desiredFrameHeight);
Mind that the combination of width and height must be one of the supported picture formats for your camera, otherwise it will just get a black image. You can get those through Camera.Parameters.getSupportedPictureSizes().
However, setting a high resolution appears to exceed the YUV conversion buffer's capacity, so I'm still struggling with that. I'm going to make a new separate question for that, to keep everything clearer: new thread
setPreviewSize does not set picture resolution. setPictureSize does.
I want to write an activity that:
Shows the camera preview (viewfinder), and has a "capture" button.
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:
Use the android.provider.MediaStore.ACTION_IMAGE_CAPTURE event. See this question
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+.
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.
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:
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.
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.
"the recommended way to access the camera is to open Camera on a separate thread". Otherwise, Camera.open() can take a while and might bog down the UI thread.
"Callbacks will be invoked on the event thread open(int) was called from". That's why to achieve best performance with camera preview callbacks (e.g. to encode them in a low-latency video for live communication), I recommend to open camera in a new HandlerThread, as shown here.
so, the way I read the documentation, using EXTRA_OUTPUT tells the camera to save the file in a specific location. That's great, but it also says to get a full size image. That's not so great.
How can I get just a small image but still specify the filename?
After trying to work with the built-in Camera activity for some time now I can advise you not to expect anything good from it because:
built-in activity differs from version to version. For example in 2.2 emulator it even crashes when you try to take a (dummy) picture.
Camera activity on real devices like Samsung Galaxy S is different, i.e. it not just looks different, it has different code and set of bugs.
Original built-in Camera activity has CROP feature, but it is not part of the public API and thus it is not good idea to use it.
So far I fount that to be safe when working with camera I need to:
- create my custom camera activity that misses the fancy stuff like filters, etc but is more configurable (I don't have it yet). I've tried to find third party Camera App but every one of them seems to be targeted at normal users not developers, i.e. has many "cool" features but it is slow / bloated / buggy / has bad UI.
- create thumbnail images by myself outside of the Camera activity (for more control).
I really hope that I am missing something here and someone will correct me in the comments with appropriate solution...
I ended up just dealing with the large images by always scaling on the read. It would have been nice not to have to do that as I read in more than one place, but ...oh well...
problem solved, although far from elegant.