How to programmatically change contrast of a bitmap in android?

前端 未结 8 2031
眼角桃花
眼角桃花 2020-12-02 17:35

I want to programmatically change the contrast of bitmap. Till now I have tried this.

private Bitmap adjustedContrast(Bitmap src, double value)
    {
                


        
8条回答
  •  Happy的楠姐
    2020-12-02 18:04

    Here is a Renderscript implementation (from the Gradle Example Projects)

    ip.rsh

    #pragma version(1)
    #pragma rs java_package_name(your.app.package)
    

    contrast.rs

    #include "ip.rsh"
    
    static float brightM = 0.f;
    static float brightC = 0.f;
    
    void setBright(float v) {
        brightM = pow(2.f, v / 100.f);
        brightC = 127.f - brightM * 127.f;
    }
    
    void contrast(const uchar4 *in, uchar4 *out)
    {
    #if 0
        out->r = rsClamp((int)(brightM * in->r + brightC), 0, 255);
        out->g = rsClamp((int)(brightM * in->g + brightC), 0, 255);
        out->b = rsClamp((int)(brightM * in->b + brightC), 0, 255);
    #else
        float3 v = convert_float3(in->rgb) * brightM + brightC;
        out->rgb = convert_uchar3(clamp(v, 0.f, 255.f));
    #endif
    }
    

    Java

    private Bitmap changeBrightness(Bitmap original RenderScript rs) {
        Allocation input = Allocation.createFromBitmap(rs, original);
        final Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptC_contrast mScript = new ScriptC_contrast(rs);
        mScript.invoke_setBright(50.f);
        mScript.forEach_contrast(input, output);
        output.copyTo(original);
        return original;
    }
    

提交回复
热议问题