package com.printer.demo.utils;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.Color;

public class ImageBinarizationUtils {

    /**
     * 使用固定阈值法对图像进行二值化
     *
     * @param originalBitmap 原始图像
     * @param threshold 阈值
     * @return 二值化后的图像
     */
    public static Bitmap applyFixedThresholding(Bitmap originalBitmap, int threshold) {
        int width = originalBitmap.getWidth();
        int height = originalBitmap.getHeight();
        Bitmap binaryBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        // 批量获取所有像素值
        int[] pixels = new int[width * height];
        originalBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        // 遍历所有像素并进行二值化
        for (int i = 0; i < pixels.length; i++) {
            int grayValue = Color.red(pixels[i]); // 假设是灰度图
            int binaryValue = grayValue > threshold ? 255 : 0;
            pixels[i] = Color.rgb(binaryValue, binaryValue, binaryValue);
        }

        // 批量设置像素值
        binaryBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return binaryBitmap;
    }

    /**
     * 使用 Otsu 大津法对图像进行二值化
     *
     * @param originalBitmap 原始图像
     * @return 二值化后的图像
     */
    public static Bitmap applyOtsuThresholding(Bitmap originalBitmap) {
        int width = originalBitmap.getWidth();
        int height = originalBitmap.getHeight();
//        Bitmap binaryBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        int[] pixels = new int[width * height];
        originalBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        int[] histogram = new int[256];
        int total = width * height;

        // 构建直方图
        for (int pixel : pixels) {
            int grayValue = Color.red(pixel); // 假设为灰度图
            histogram[grayValue]++;
        }

        // 计算最佳阈值
        int sum = 0;
        for (int i = 0; i < 256; i++) {
            sum += i * histogram[i];
        }
        int sumB = 0, wB = 0, wF = 0;
        float maxVariance = 0;
        int threshold = 0;
        for (int i = 0; i < 256; i++) {
            wB += histogram[i];
            if (wB == 0) continue;
            wF = total - wB;
            if (wF == 0) break;
            sumB += i * histogram[i];
            float mB = (float) sumB / wB;
            float mF = (float) (sum - sumB) / wF;
            float betweenVariance = (float) wB * wF * (mB - mF) * (mB - mF);
            if (betweenVariance > maxVariance) {
                maxVariance = betweenVariance;
                threshold = i;
            }
        }

        // 使用最佳阈值进行二值化
        return applyFixedThresholding(originalBitmap, threshold);
    }


    /**
     * 选择使用不同的算法对图像进行二值化
     *
     * @param originalBitmap 原始图像
     * @param method 二值化方法（"fixed", "otsu", "floyd"）
     * @param threshold 固定阈值方法的阈值，仅在选择 "fixed" 方法时有效
     * @return 二值化后的图像
     */
    public static Bitmap binarizeImage(Bitmap originalBitmap, String method, int threshold) {
        switch (method.toLowerCase()) {
            case "fixed":
                return applyFixedThresholding(originalBitmap, threshold);
            case "otsu":
                return applyOtsuThresholding(originalBitmap);
            default:
                throw new IllegalArgumentException("Unknown binarization method: " + method);
        }
    }
}
