Android Question How to reduce camera photo file size dramatically

toby

Well-Known Member
Licensed User
Longtime User
I use this Camera2 example to take snapshots every second and save the snapshots as jpg files to the internal storage. The file size is about 351Kbytes each, meaning that 1.2G of space is needed in a single hour, and the phone would run out of storage quickly. I would like to sacrifice snapshot quality for reduced file size as long as the picture is still clear.

Could someone kindly give me some hints on how to achieve that?


TIA
 
Last edited:

emexes

Expert
Licensed User
sacrifice snapshot quality for reduced file size as long as the picture is still clear

If your application is time-lapse photography, or a mostly-stationary security camera, or any scene that doesn't change often, then:

Another approach might be to compare/difference/subtract the new snapshot bitmap with the previously-saved snapshot bitmap, and then only saving the new snapshot if it is sufficiently different.

The difference could be defined as the average of the pixel color component differences, or their squares, or the largest difference. Also, if there are areas of interest in the snapshots, then you could just monitor those areas, rather than the entire snapshot field of view.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Another approach might be to compare/difference/subtract the new snapshot bitmap with the previously-saved snapshot bitmap, and then only saving the new snapshot if it is sufficiently different.
Interesting idea indeed and I smell AI!

My app is to be mounted on a moving vehicle. I'll keep your tip in mind for my future projects.
 
Upvote 0

emexes

Expert
Licensed User
My app is to be mounted on a moving vehicle.

JPEG joking aside... there are additional ways of reducing down the long-term size of your image dataset even further.

eg if you are aiming to capture out-of-the-ordinary events, then you could reduce uneventful driving to smaller more-compression images per above, but if the accelerometers detect change in speed more than a threshold eg heavy braking or sideways movement or sharp turns or speed bumps (or rollovers?!) then keep those more-eventful images in their original hi-res hi-quality glory.

Or if you're using it as a driving logbook, then instead of having an hour of history at one image per second, you could start to drop the older images, eg:

30 minutes at image every second = 1800 images
+ 30 minutes keeping every 2nd image = 900 images
+ 30 minutes keeping every 4th image = 450 images
+ 30 minutes keeping every 8th image = 225 images
+ 30 minutes keeping every 16th image = 113 images
+ 30 minutes keeping every 32nd image = 56 images
+ 30 minutes keeping every 64th image = 28 images

and in theory that sum of a series will extend infinitely but in practice keeping eg every 2^16th image is probably past point of being useful.

You could have a save-this-for-evidence button that, when you press it, locks in the last couple of minutes of images that are still filed away at one image every second, ideally still in original resolution and quality.

Which reminds me: perhaps save the images originally in full quality, and then squish them down say five minutes later, ie every time you save a new image, also recompress the image saved five minutes earlier. Or recompress them when you're doing the first culling ie no need to recompress images that you're throwing out anyway. Or keep a five-minute loop of the original images, in addition to recompressing and archiving them to the long-term history.

Another idea I had is that video compression of a series of related changing images is even better than single-image compression, because it can borrow similar blocks of pixels from nearby frames, which is way more efficient than storing the same pixel patterns repeatedly. You could do that by saving say 300 images (5 minutes) of images and then doing a (motion-)JPEG to MPEG/etc conversion to a 12-second video.

I've only ever done this by calling FFMPEG externally under Windows, but presumably you'd do it with a library within your B4X program, eg check here:

B4Xgoodies_from_walt61 Online
The direct link of the sheet is given in Post#21
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…