Android Question Photo brightness/contrast through seekbar

yiankos1

Well-Known Member
Licensed User
Longtime User
Goodmorning,
I am developing an app, that i want to implement some "filters" like contrast/brightness/hue etc. All these changes i want to get done through seekbar valuechanged. When i try the code below, it works but it is not smooth at all for realtime change through seekbar and it is not optimized for 12mpxl and above photos. I want something like instagram's manual adjustments, that is perfectly smooth.
Thanks for your time.
B4X:
Sub SeekBar2_ValueChanged (Value As Int, UserChanged As Boolean)
    Dim In As InputStream
    Dim c1,cr As Canvas
    Dim r As Rect
    Dim bmp1, bmpr As Bitmap   

    ImageView2.SetBackgroundImage(Null)
    ImageView2.Gravity = Gravity.fill
    c1.Initialize(ImageView1)
    cr.Initialize(ImageView2)
    ImageView2.Gravity = Gravity.fill
    In = File.OpenInput(File.DirAssets, ImageName)
    Jpg1.LoadJpegSizeOnly(In)
    In = File.OpenInput(File.DirAssets, ImageName)   
    bmp1 = Jpg1.LoadJpegSmaller(In, 1)
    bmpr.InitializeMutable(Jpg1.JpegWidth,Jpg1.JpegHeight)
   
   
    Jpg1.SetPixelsFromBmp(bmp1)
    Jpg1.PixelsABGRtoARGB
    Jpgr.SetPixelsFromBmp(bmpr)
'    Jpgr.PixelsABGRtoARGB
   
    Dim pixelval, alpha, rd, gn, bl As Double
    Dim c As Float
    Dim t As Int
   
    t = SeekBar1.value - 200 + 100
    c = Power(((100 + t) / 100),2)
   
    For x = 0 To Jpg1.BmpWidth - 1
      For y = 0 To Jpg1.BmpHeight - 1
        pixelval = Jpg1.GetBmpPixel(x, y)
        alpha = Bit.UnsignedShiftRight(Bit.AND(pixelval, 0xff000000), 24)
        rd = Bit.UnsignedShiftRight(Bit.AND(pixelval, 0xff0000), 16)
        gn = Bit.UnsignedShiftRight(Bit.AND(pixelval, 0xff00), 8)
        bl = Bit.AND(pixelval, 0xff)
       
        rd = ((((rd / 255) - 0.5) * c) + 0.5) * 255
        If rd > 255 Then rd = 255
        If rd < 0 Then rd  = 0
        gn = ((((gn / 255) - 0.5) * c) + 0.5) * 255
        If gn > 255 Then gn = 255
        If gn < 0 Then gn  = 0       
        bl = ((((bl / 255) - 0.5) * c) + 0.5) * 255
        If bl > 255 Then bl = 255
        If bl < 0 Then bl  = 0       
        Jpgr.SetBmpPixel(x, y, Colors.ARGB(Bit.AND(alpha,255),Bit.AND(rd,255), Bit.AND(gn,255), Bit.AND(bl,255)))
      Next
    Next

    bmpr = Jpgr.GetBmpFromPixels
    In.Close

    r.Initialize(0, 0, ImageView1.Width, ImageView1.Height)
    c1.DrawBitmap(bmp1, Null, r)
    ImageView1.Invalidate

   
    ImageView2.Gravity = Gravity.FILL
    r.Initialize(0, 0, ImageView1.Width, ImageView1.Height)
    cr.DrawBitmap(bmpr, Null, r)
    ImageView2.Invalidate
End Sub
 
Top