Reputation: 165
I'm using Camera2 api to do a still image capture and save it to a jpeg file. The problem is that the size of the file is always >900kb, even if I set the image dimensions to the smallest available and put jpeg quality low.
This is how I'm saving the file in the ImageAvailableListener
. It's a xamarin project so code is in c#.
image = reader.AcquireLatestImage();
ByteBuffer buffer = image.GetPlanes()[0].Buffer;
byte[] bytes = new byte[buffer.Remaining()];
buffer.Get(bytes);
output = new FileOutputStream(File);
output.Write(bytes);
output.Close();
The file should be ~20kb, so why can't I get file sizes lower than 900kb?
Upvotes: 2
Views: 1900
Reputation: 7557
You can also reduce the capture image quality
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.set(CaptureRequest.JPEG_THUMBNAIL_QUALITY, (byte) 70); // add this line and set your own quality
Upvotes: 2
Reputation: 165
I figured it out. Needed to create a bitmap to apply compression:
image = reader.AcquireLatestImage();
ByteBuffer buffer = image.GetPlanes()[0].Buffer;
byte[] bytes = new byte[buffer.Remaining()];
buffer.Get(bytes);
// need to get the bitmap in order to compress
Bitmap bitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 85, stream);
Save(stream.GetBuffer());
}
Upvotes: 1