feat: 添加 ImageScanner 图片扫描工具

实现了文件夹扫描和图片索引功能

- 扫描指定文件夹中的图片文件
- 支持 jpg、jpeg、png、webp、bmp 格式
- 使用 JSON 缓存避免重复扫描
- 支持按索引获取图片
- 支持随机获取图片(可排除当前图片)
- 统计总图片数量

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-20 23:58:49 +08:00
1 parent b63156cda9
commit 78d33cc460
1 file changed
+133
@@ -0,0 +1,133 @@
package cn.hugh.bz.gk2.utils;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import cn.hugh.bz.gk2.model.WallpaperImage;
public class ImageScanner {
private static final String TAG = "ImageScanner";
private static final List<String> IMAGE_EXTENSIONS = List.of("jpg", "jpeg", "png", "webp", "bmp");
private final Context context;
private final JsonHelper jsonHelper;
public ImageScanner(Context context) {
this.context = context;
this.jsonHelper = new JsonHelper(context);
}
public List<WallpaperImage> scanFolder(String folderPath) {
if (!jsonHelper.needsRescan(folderPath)) {
List<WallpaperImage> cached = jsonHelper.loadImageIndex(folderPath);
if (cached != null) {
Log.i(TAG, "Using cached index for: " + folderPath);
return cached;
}
}
Log.i(TAG, "Scanning folder: " + folderPath);
File folder = new File(folderPath);
if (!folder.exists() || !folder.isDirectory()) {
Log.e(TAG, "Folder not found or not a directory: " + folderPath);
return new ArrayList<>();
}
List<WallpaperImage> images = new ArrayList<>();
File[] files = folder.listFiles();
if (files == null) {
return images;
}
for (File file : files) {
if (file.isFile() && isImageFile(file)) {
WallpaperImage image = new WallpaperImage(
file.getAbsolutePath(),
folderPath,
file.getName(),
file.lastModified(),
file.length()
);
images.add(image);
}
}
// Save to JSON
jsonHelper.saveIndex(folderPath, images);
Log.i(TAG, "Found " + images.size() + " images in " + folderPath);
return images;
}
private boolean isImageFile(File file) {
String name = file.getName().toLowerCase();
for (String ext : IMAGE_EXTENSIONS) {
if (name.endsWith("." + ext)) {
return true;
}
}
return false;
}
public WallpaperImage getImageAtIndex(List<String> folders, int index) {
List<WallpaperImage> allImages = loadAllImages(folders);
if (allImages.isEmpty() || index < 0 || index >= allImages.size()) {
return null;
}
return allImages.get(index);
}
public WallpaperImage getRandomImage(List<String> folders, String excludeUri) {
List<WallpaperImage> allImages = loadAllImages(folders);
if (allImages.isEmpty()) {
return null;
}
// Filter out excluded image
List<WallpaperImage> candidates = new ArrayList<>();
for (WallpaperImage image : allImages) {
if (!image.getUri().equals(excludeUri)) {
candidates.add(image);
}
}
if (candidates.isEmpty()) {
// All images are the same as excluded, return any
return allImages.get(0);
}
Random random = new Random();
return candidates.get(random.nextInt(candidates.size()));
}
public int getTotalImageCount(List<String> folders) {
return loadAllImages(folders).size();
}
private List<WallpaperImage> loadAllImages(List<String> folders) {
List<WallpaperImage> allImages = new ArrayList<>();
for (String folder : folders) {
List<WallpaperImage> images = jsonHelper.loadImageIndex(folder);
if (images != null) {
allImages.addAll(images);
} else {
// Scan if no cache
allImages.addAll(scanFolder(folder));
}
}
return allImages;
}
}