I want to get a full resolution(and not compressed)image data, then i can do some image processing.
As far as i know, the android api takePicture (shutter, raw, jpg) can do something.But what i need is not a compressed JPEG data, a uncompressed image data instead.Also I knew the raw callback doesn't work according to some posts i have read.
I also found the api onPreviewFrame, and the larggest picture size i got from this is 1280*720(use setPreviewSize) while the original image i caputure from the camera is a resolution of 1952*3264.
Also, Intent.putExtra(MediaStore.EXTRA_OUTPUT, uri) may be help, but it likely that the file of the uri should be a jpeg file which is a compressed format.
But is there anyway to get a full size(the same as captured) and uncompressed(not a JPEG) image data?
The documentation for takePicture() clearly says that raw data can be requested, but the callback is optional. Today, most of devices do not support raw callback. This should not be a surprise: modern cameras perform Jpeg compression in hardware, and the memory bus between the camera and the application processor cannot handle 24 Megabyte of raw data fast enough (for a modest 8 megapixel camera).
Avoid temptation to use preview callback instead of takePicture(): even at same resolution, image qualiity of a still picture will be better. Preview image may have imprecise autofocus, stabilization, exposure and even white balance.
Related
I need to send a picture from my app to the server. However before I send it I need to do some little modifications on it like rotating it. My problem is to know what quality to save it before sending it to the server. I tried
bitmap.compress(CompressFormat.JPEG, 100, fileoutputstream);
file size before: 1.5mb - file size after 3 mb :(
so it seems that the file is now bigger than before and I'm not sure that the quality is better (as the image was already compressed). Now just doing
bitmap.compress(CompressFormat.JPEG, 80, fileoutputstream);
file size before: 1.5mb - file size after 0.3 mb
so surely with a quality of 80 I will have a lesser quality image than before. So what quality does android use by default when a user takes a picture and saves it to the gallery? How do I save the image with the same quality as before, without losing anything?
For an app like instagram/500px what is an acceptable image quality we can set before sending picture to the server? 50? 75? 80? 100?
Every saving in JPEG is cost to you some quality loss. Even if you use "100% quality", some lossy compression will occur.
How to save the image with the same quality as before, without loosing
anything ?
You can't save jpeg without quality loss. Only very specialized software can do some tricks without recompression, with very limited features like rotation by 90 degree.
so surelly with a quality of 80 i will have a less quality than before
So what quality does android use by default
JPEG standard not defines quality in percents at all. If you use "80%" in one program, it can be same as "50%" or "90%" in other. This is just numbers for some encoder.
JPEG and PNG are both compress mode. So, it can not make sure the 100% quality.
For JPEG, in my case, i often use 90% for high quality, use 70% for low quality. In opencv, for CV_IMWRITE_JPEG_QUALITY, default value is 90% quality.
Hope this is helpful.
I am using this method for compressing the image https://gist.github.com/ManzzBaria/c3af85b708fee49d55f7
but my images are loosing quality after compression. it is worse when an image is take from phone and compressed.
Please let me know where I am doing wrong.
Reducing the size of the image takes pixels away, effectively reducing the resolution of the image. Of course, the file also gets smaller. If you save as a high-fidelity JPEG (say 95%), this may be all the file size reduction you need.
Reducing the size of the file with a lossless method (eg PNG) will achieve a reduction in file size with no loss of quality — hence the name. The reduction depends on the image: a picture with many identical pixels, eg a screenshot of this page, will compress substantially.
Using a lossy method (eg normal JPEG compression) will result in an even smaller file, but you will pay for this reduction with a loss of quality. Most methods of saving JPEGs allow you to choose a fidelity, often 80% by default, which controls the quality of the resulting image; this is the line that does it in the code you posted:
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
Again, the reduction you achieve is inversely proportional to the complexity of the image, as well as to this fidelity parameter.
Here's a blog post I wrote on choosing image formats: How to choose an image format. This one might also be worth reading.
There are more good links on lossy vs lossless compression in #Gavriel's answer.
You should read about lossy and lossless image compression:
Lossless and lossy image compression algorithm?
PHP Compress Image with Lossy/Lossless
jpeg compression - lossy or lossless
I'm using PreviewDisplay to create custom camera app, and onPreviewFrame callback to manipulate each frame (in my case, send image to server once in pre-defined number of frames while keep displaying smooth video stream to the user).
The highest resolution returned by getSupportedPreviewSizes is lower than the best resolution of images captured by built in camera application.
Is there any way to get the frames in best resolution as achieved by built in camera application?
Try getSupportedVideoSizes(), also getPreferredPreviewSizeForVideo().
Note that in some cases, the camera may be able to produce higher res frames, and pipe them to the hardware encoder for video recording, but not have bandwidth to push them to onPreviewFrame() callback.
I am trying to use data from Android picture. I do not like JPEG format, since eventually I will use gray scale data. YUV format is fine with me, since the first half part is gray-scale.
from the Android development tutorial,
public final void takePicture (Camera.ShutterCallback shutter,
Camera.PictureCallback raw, Camera.PictureCallback postview,
Camera.PictureCallback jpeg)
Added in API level 5
Triggers an asynchronous image capture. The camera service will
initiate a series of callbacks to the application as the image capture
progresses. The shutter callback occurs after the image is captured.
This can be used to trigger a sound to let the user know that image
has been captured. The raw callback occurs when the raw image data is
available (NOTE: the data will be null if there is no raw image
callback buffer available or the raw image callback buffer is not
large enough to hold the raw image). The postview callback occurs when
a scaled, fully processed postview image is available (NOTE: not all
hardware supports this). The jpeg callback occurs when the compressed
image is available. If the application does not need a particular
callback, a null can be passed instead of a callback method.
It talks about "the raw image data". However, I find nowhere information about the format for the raw image data?
Do you have any idea about that?
I want to get the gray-scale data of the picture taken by the photo, and the data are located in the phone memory, so it would not cost time to write/read from image files, or convert between different image formats. Or maybe I have to sacrifice some to get it??
After some search, I think I found the answer:
From the Android tutorial:
"The raw callback occurs when the raw image data is available (NOTE:
the data will be null if there is no raw image callback buffer
available or the raw image callback buffer is not large enough to hold
the raw image)."
See this link (2011/05/10)
Android: Raw image callback supported devices
Not all devices support raw pictureCallback.
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/ZRkeoCD2uyc (2009)
The employee Dave Sparks at Google said:
"The original intent was to return an uncompressed RGB565 frame, but
this proved to be impractical. " "I am inclined to deprecate that API
entirely and replace it with hooks for native signal processing. "
Many people report the similar problem. See:
http://code.google.com/p/android/issues/detail?id=10910
Since many image processing processes are based on gray scale images, I am looking forward gray scale raw data in the memory produced for each picture by the Android.
You may have some luck with getSupportedPictureFormats(). If it lists some YUV format, you can use setPictureFormat() and the desired resolution, and ciunterintuitively you will get the uncompressed high quality image in JpegPreview callback, from which grayscale (a.k.a. luminance) can be easily extracted.
Most devices will only list JPEG as a valid choice. That's because they perform compression in hardware, on the camera side. Note that the data transfer from camera to application RAM is often the bottleneck; if you can use stagefright hw JPEG decoder, you will actually get the result faster.
The biggest problem with using the raw callback is that many developers have trouble with getting anything returned on many phones.
If you are satisfied with just the YUV array, your camera preview SurfaceView can implement PreviewCallback and you can add the onPreviewFrame method to your class. This function will allow you direct access to the YUV array for every frame. You can fetch it when you choose.
EDIT: I should specify that I was assuming you were building a custom camera application in which you extended SurfaceView for a custom camera preview surface. In order to follow my advice you will need to build a custom camera. If you are trying to do things quickly though I suggest building a new bitmap out of the JPEG data where you implement the greyscale yourself.
I would like to capture an image with the Android Camera but because the image may contain sensitive data I dont want the image saved to the phone or sd card. Instead I would like a base64 string (compressed) which would be sent to the server immediately
In PhoneGap it seems files are saved to various places automatically.
Natively I was never able to get the image stream - in onJpegPictureTaken() the byte[] parameter was always null.
can anyone suggest a way?
See Camera.onPreviewFrame() and the YuvImage.compresstoJpeg() to be able to get a byte array you can convert into a bitmap.
Note that YuvImage.compressToJpeg() is only available in SDK 8 or later, I think. For earlier versions you'll need to implement your own YUV decoder. There are several examples around or, I could provide you an example.
Those two methods will allow you to get a camera picture in memory and never persist it to SD. Beware that bitmaps of most camera preview sizes will chew up memory pretty quickly and you'll need to be very careful to recycle the bitmaps and probably also have to scale them down a bit to do much with them and still fit inside the native heap restrictions on most devices.
Good luck!