Android Code Snippet Photo filters Seekbar

Hello to my friends,
Here is my first Code Snippet. It simply adds filters - effects to photo - images and changes value of filter through seekbar. Here is the in-line java code for filters:

In-line Java code:
B4X:
#If Java
import android.graphics.*;


    /**
     *
     * @param bmp input bitmap
     * @param contrast 0..10 1 is default
     * @param brightness -255..255 0 is default
     * @return new bitmap
     */
    public Bitmap changeBitmapContrast(Bitmap bmp, float contrast)
    {
        float scale = contrast + 1.f;
        float translate = (-.5f * scale + .5f) * 255.f;   
        ColorMatrix cm = new ColorMatrix(new float[]
         {
            scale, 0, 0, 0, translate,
            0, scale, 0, 0, translate,
            0, 0, scale, 0, translate,
            0, 0, 0, 1, 0
        });

        Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

        Canvas canvas = new Canvas(ret);

        RectF drawRect=new RectF();
        drawRect.set(0,0,bmp.getWidth(),bmp.getHeight());

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bmp, null, drawRect, paint);

        return ret;
    }
   
        public Bitmap changeBitmapBrightness(Bitmap bmp, float value)
    {
        ColorMatrix cm = new ColorMatrix(new float[]
                {
                    1, 0, 0, 0, value,
                    0, 1, 0, 0, value,
                    0, 0, 1, 0, value,
                    0, 0, 0, 1, 0,
                    0, 0, 0, 0, 1
                });
       
        Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

        Canvas canvas = new Canvas(ret);

        RectF drawRect=new RectF();
        drawRect.set(0,0,bmp.getWidth(),bmp.getHeight());

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bmp, null, drawRect, paint);

        return ret;
    }
   
    public Bitmap changeBitmapHue(Bitmap bmp, float value)
    {
        float cosVal = (float) Math.cos(value);
        float sinVal = (float) Math.sin(value);
        float lumR = 0.213f;
        float lumG = 0.715f;
        float lumB = 0.072f;
        ColorMatrix cm = new ColorMatrix(new float[]
            {
                lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
                lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0,
                0f, 0f, 0f, 1f, 0f,
                0f, 0f, 0f, 0f, 1f
            });

        Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

        Canvas canvas = new Canvas(ret);

        RectF drawRect=new RectF();
        drawRect.set(0,0,bmp.getWidth(),bmp.getHeight());

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bmp, null, drawRect, paint);

        return ret;
    }
   
    public Bitmap changeBitmapSaturation(Bitmap bmp, float value)
    {
        float x = 1 + ((value > 0) ? 3 * value / 100 : value / 100);
        float lumR = 0.3086f;
        float lumG = 0.6094f;
        float lumB = 0.0820f;
        ColorMatrix cm = new ColorMatrix(new float[]
                    {
                   lumR * (1 - x) + x, lumG * (1 - x), lumB * (1 - x), 0, 0,
                    lumR * (1 - x), lumG * (1 - x) + x, lumB * (1 - x), 0, 0,
                    lumR * (1 - x), lumG * (1 - x), lumB * (1 - x) + x, 0, 0,
                    0, 0, 0, 1, 0,
                    0, 0, 0, 0, 1
                    });
               
        Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

        Canvas canvas = new Canvas(ret);

        RectF drawRect=new RectF();
        drawRect.set(0,0,bmp.getWidth(),bmp.getHeight());

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bmp, null, drawRect, paint);

        return ret;
    }
   
        public Bitmap changeBitmapTemperature(Bitmap bmp, int r, int g, int b)
    {
        ColorMatrix cm = new ColorMatrix(new float[]
                {
            r / 255.0f, 0, 0, 0, 0,
            0, g / 255.0f, 0, 0, 0,
            0, 0, b / 255.0f, 0, 0,
            0, 0, 0, 1, 0
                });
   
        Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

        Canvas canvas = new Canvas(ret);

        RectF drawRect=new RectF();
        drawRect.set(0,0,bmp.getWidth(),bmp.getHeight());

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bmp, null, drawRect, paint);

        return ret;
    }
   
            public Bitmap changeBitmapExposure(Bitmap bmp, float value)
    {
        ColorMatrix cm = new ColorMatrix(new float[]
                {
            value, 0, 0, 0, 0,
            0, value, 0, 0, 0,
            0, 0, value, 0, 0,
            0, 0, 0, 1, 0,
                });
   
        Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

        Canvas canvas = new Canvas(ret);

        RectF drawRect=new RectF();
        drawRect.set(0,0,bmp.getWidth(),bmp.getHeight());

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bmp, null, drawRect, paint);

        return ret;
    }

#End If

You just have to call this Java code and just put source bitmap and the value of filter(seekbar value):

Initialize Java object:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    NativeMe.InitializeContext 'Initialize java object
End sub

Change value of filter through Seekbar:
B4X:
Sub SeekBar1_ValueChanged (Value As Int, UserChanged As Boolean)

    Dim c As Float
    c = Value 'We do this, cause filter need float number not int

    imgPhoto.Bitmap=Null

    bm1 = NativeMe.RunMethod("changeBitmapBrightness", Array(bm,c)) 'We call java object
   
    Try
        imgPhoto.Bitmap=bm1 'We put the result at imageview
    Catch
        ToastMessageShow("Unknown problem has occured!",False)
    End Try

End Sub

Available methods:
  1. changeBitmapContrast
  2. changeBitmapBrightness
  3. changeBitmapHue
  4. changeBitmapSaturation
  5. changeBitmapTemperature
  6. changeBitmapExposure
Comments:
  • Smooth results will only be in release mode(not debug).
  • Enable JavaObject library
 

ddk1

Member
Licensed User
Brilliant thanks yiankos1.
For anyone else using this a couple of notes.
Need this code:
B4X:
Sub Process_Globals
    Private NativeMe As JavaObject
End Sub

The java code in the first post just needs to be pasted at the bottom of your activity.
The allowed range of values for contast and brightness are listed at the top of the java code:
Contrast are 0..10 1 is default, Brightness -255..255 0 is default
Not sure about the other functions.
 
Top