TL;DR: CleanMaster is a free iOS & Android app that frees up phone storage: it finds duplicate and similar photos, keeps the best shot, clears old screenshots, chat-app media and big videos, and locks private files in a vault. Everything runs on the device. This post is how the photo part works under the hood.
๐ฑ App Store ยท ๐ค Google Play
Phones fill up with near-copies: the same meme saved three times, a forwarded photo that came back from WhatsApp, a burst of 12 shots where only one has everyone's eyes open.
While building CleanMaster, a storage cleaner for iOS and Android, we set two rules: nothing leaves the phone, and nothing is deleted that the user hasn't reviewed. That ruled out sending photos to a cloud model. Here's how the on-device pipeline works, with the actual code.
1. Fingerprinting photos with difference hashes
To find "the same picture saved more than once", we need a fingerprint that survives re-compression and resizing (what chat apps do to a forwarded photo) but changes when the picture really changes.
A difference hash (dHash) fits well: shrink the image to a tiny grayscale grid, then store one bit per pixel saying whether it is brighter than its right-hand neighbour. Re-encoding a JPEG barely moves those comparisons; cropping or editing does.
We compute two of them in a native Expo module (Swift on iOS, Kotlin on Android):
- a coarse 64-bit hash (9ร8 grid) to gather candidates quickly
- a fine 256-bit hash (17ร16 grid) to make the final decision
The thresholds are deliberately tight:
/**
* Differing bits allowed. The coarse 64-bit hash only gathers candidates; the
* 256-bit one decides. Kept tight: two different screenshots of the same app
* screen look alike at this size, and a false match means a wrong deletion.
*/
const COARSE_MAX = 3;
const FINE_MAX = 8;
/** Copies keep their shape; a different aspect ratio is a different picture (or a crop). */
const ASPECT_TOLERANCE = 0.02;
/** Fewer set (or unset) bits than this in the 256-bit hash means a near-flat picture. */
const MIN_DETAIL_BITS = 24;
The last constant matters more than it looks. Black frames, white walls and pocket shots all hash to almost the same value, so "near-flat" pictures are excluded from duplicate matching entirely.
Comparing hashes is a Hamming distance. We split the hex string into 32-bit words and popcount the XOR, bailing out as soon as the limit is passed:
function popcount(v: number) {
v -= (v >>> 1) & 0x55555555;
v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);
return (((v + (v >>> 4)) & 0x0f0f0f0f) * 0x01010101) >>> 24;
}
function distance(a: Int32Array, b: Int32Array, max: number) {
let d = 0;
for (let i = 0; i < a.length && d <= max; i++) d += popcount(a[i] ^ b[i]);
return d;
}
Hashes are cached by asset ID in the app's cache folder (they can always be recomputed), and we hash four photos at a time. A nice side effect on iOS: the small local thumbnail of an iCloud-only photo hashes the same as the full image, so we don't need to download anything.
2. Deciding which copy to keep
For true duplicates the rule is simple: keep the copy with the most pixels, and if that's a tie, the oldest one.
function better(a: WinItem, b: WinItem) {
const pa = a.width * a.height;
const pb = b.width * b.height;
if (pa !== pb) return pa > pb;
const ta = a.createdAt || Infinity;
const tb = b.createdAt || Infinity;
return ta !== tb ? ta < tb : a.id < b.id;
}
3. Best Shot: picking the winner in a burst
Bursts are harder. The shots aren't duplicates; they're different moments a fraction of a second apart, and "keep the first one" is often wrong. So the native module scores each photo:
- Sharpness: variance of the Laplacian on a grayscale copy. Blur flattens edges, so the variance drops.
- Exposure: mean brightness plus the share of clipped (blown-out or crushed) pixels.
-
Faces: on iOS, Vision's
VNDetectFaceCaptureQualityRequestplus face landmarks, where we compute an eye aspect ratio (about 0.3 is wide open, under 0.1 is shut). On Android, ML Kit face detection's eye-open probability. The worst face in the frame counts, because one person blinking ruins a group shot.
Then each burst is ranked relative to itself:
const maxSharp = Math.max(...qs.map((q) => q.sharpness), 1);
const maxFaces = Math.max(...qs.map((q) => q.faces));
// ...
const sharp = q.sharpness / maxSharp;
const exposure = Math.max(0, 1 - Math.abs(q.brightness - 0.5) * 1.6 - q.clipped);
if (!maxFaces) return { item, total: 0.7 * sharp + 0.3 * exposure, q };
const signals = [q.eyesOpen, q.faceQuality].filter((v): v is number => v != null);
// Someone missing from the frame (turned away, cropped) counts against it.
const face = (signals.length ? signals.reduce((a, b) => a + b, 0) / signals.length : 0.5) * (q.faces / maxFaces);
return { item, total: 0.4 * sharp + 0.4 * face + 0.2 * exposure, q };
Two details made a real difference:
- Sharpness is relative to the sharpest shot in the same burst. A dim restaurant photo is never as "sharp" as a sunny beach, and it shouldn't be punished for that.
-
faces / maxFacespenalises the frame where someone turned away or walked out of shot.
The app also shows why a photo won ("Sharpest ยท eyes open"), and you can tap to compare before deleting. People trust a cleaner far more when it explains itself.
What we learned
- Bias towards false negatives. Missing a duplicate costs a few MB; a wrong match costs someone a photo they cared about.
- Make caches recomputable and store them where the OS is allowed to purge them.
- Native modules pay off. Hashing and scoring in Swift/Kotlin through Expo modules, with JS only orchestrating, kept scans of thousands of photos usable on mid-range Android phones.
- On-device is good enough. Apple Vision and Google ML Kit cover faces and eyes for free, with no privacy questions to answer.
Try it
CleanMaster is free on both stores. It also cleans old screenshots, chat-app media and big videos, and includes a PIN-locked private vault:
- iPhone: https://apps.apple.com/us/app/clean-free-up-your-space/id6818406418
- Android: https://play.google.com/store/apps/details?id=com.nexa.free_up_your_space
Happy to answer questions about the hashing or the scoring in the comments.
Top comments (0)