Android Question Save lowers quality

yiankos1

Well-Known Member
Licensed User
Longtime User
Hello my friends,
I use ABExtDrawing library in order to change saturation,brightness,contrast etc of a photo with this code(all done by a seekbar):

B4X:
Sub SeekBar1_ValueChanged (Value As Int, UserChanged As Boolean)

Private abd As ABExtDrawing

Private recta As Rect
recta.Initialize(0,0,imgPhoto.Width,imgPhoto.Height)

lblSeek1.Text="Brightness: " & c

paint.SetColorMatrixColorFilter(Array As Float(1, 0, 0, 0, c,0, 1, 0, 0, c,0, 0, 1, 0, c,0, 0, 0, 1, 0,0, 0, 0, 0, 1))
abd.drawBitmap(canvas2,bm,Null,recta,paint) 'canvas2 is a canvas to be drawn, bm is my photo bitmap, recta is as Rect where to be drawn, paint is the brightness filter

Activity.Invalidate2(recta) 'realtime seekbar change

End Sub

When user wants to apply for example that level of brightness, hits button "Apply", which does this:
B4X:
Sub btnApply_Click

    out=File.OpenOutput(File.DirInternal,Main.filename,False)
    canvas2.Bitmap.WriteToStream(out,100,"JPEG")
    out.Close

    bm=LoadBitmap(File.DirInternal,Main.filename) 'Loads the new "edited" photo at bitmap, in order user can furthermore edit it

End Sub

My question here is, if user keep editing photo and hitting Apply button, photo quality lowers dramatically. Does anyone knows any workaround for this problem?
 

LucaMs

Expert
Licensed User
Longtime User
Try using:
B4X:
SetAntiAlias(Canvas2)


Private Sub SetAntiAlias (c As Canvas)
    Dim r As Reflector
    Dim NativeCanvas As Object
    r.Target = c
    NativeCanvas = r.GetField("canvas")
    Dim PaintFlagsDrawFilter As Object
    PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", Array As Object(0, 1), Array As String("java.lang.int", "java.lang.int"))
    r.Target = NativeCanvas
    r.RunMethod4("setDrawFilter", Array As Object(PaintFlagsDrawFilter), Array As String("android.graphics.DrawFilter"))
End Sub
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Just a guess

If you are initializing your canvas to a View using canvas.initialize(...) then continue reading. If not, it doesn't apply :p


When you get canvas.Bitmap, it gives you the resolution of the View, not the one of the original bitmap

Try instead with a mutableBitmap and canvas.Initialize2(..)
B4X:
Dim bm as Bitmap
bm=LoadBitmap(File.DirInternal,Main.filename)

Dim bm2 as Bitmap
bm2.initializeMutable(bm.Width,bm.Height)
Dim Rect1 as Rect
Rect1.Initialize(0,0,bm.Width,bm.Height)
Dim cv2 as canvas
cv2.Initialize2(bm2)
cv2.DrawBitmap(bm,Null,Rect1)

IV.bitmap = bm2  'change to your ImageView name
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
Try using:
B4X:
SetAntiAlias(Canvas2)


Private Sub SetAntiAlias (c As Canvas)
    Dim r As Reflector
    Dim NativeCanvas As Object
    r.Target = c
    NativeCanvas = r.GetField("canvas")
    Dim PaintFlagsDrawFilter As Object
    PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", Array As Object(0, 1), Array As String("java.lang.int", "java.lang.int"))
    r.Target = NativeCanvas
    r.RunMethod4("setDrawFilter", Array As Object(PaintFlagsDrawFilter), Array As String("android.graphics.DrawFilter"))
End Sub
Thanks for quick answer Lucas. Your Code do i implement it after "Apply" button? Because i am little bit confused....
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
Just a guess

If you are initializing your canvas to a View using canvas.initialize(...) then continue reading. If not, it doesn't apply :p


When you get canvas.Bitmap, it gives you the resolution of the View, not the one of the original bitmap

Try instead with a mutableBitmap and canvas.Initialize2(..)
B4X:
Dim bm as Bitmap
bm=LoadBitmap(File.DirInternal,Main.filename)

Dim bm2 as Bitmap
bm2.initializeMutable(bm.Width,bm.Height)
Dim Rect1 as Rect
Rect1.Initialize(0,0,bm.Width,bm.Height)
Dim cv2 as canvas
cv2.Initialize2(bm2)
cv2.DrawBitmap(bm,Null,Rect1)

IV.bitmap = bm2  'change to your ImageView name
You helped me a lot understanding about bitmaps dimensions and i did it your way. Now resolution does not lower, but when i hit "Apply" at brightness and i go for example at saturation, there is no code to keep brightness changes, so it goes to original bitmap.
Thanks for your time
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Strange... if in your btnApply_Click you haven't removed the last line, it should load the modified bitmap again
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
Strange... if in your btnApply_Click you haven't removed the last line, it should load the modified bitmap again
B4X:
Sub btnApply_Click

    out=File.OpenOutput(Main.dir,Main.filename,False)
    bm2.WriteToStream(out,100,"JPEG")
    out.Close
   
    bm=LoadBitmap(Main.dir,Main.filename)
   
End Sub
Here is my code. bm and bm2 is dim'ed as you said. By mistake i said before that it does not save image. Problem with image quality keeps remaining. Lowers....
In my opinion, wherever i save that image, lowers quality, i think there must be a workaround for this, not keep saving that image.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
In theory, saving JPG with quality 100 as you are doing shouldn't lower quality. Anyway, just in case I would go to the gallery and see if that image size is the same as the original

Also, is "recta" still initialized to imgPhoto.width and imgPhoto.height? If so, try to change it to bm.Width and bm.Height
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
In theory, saving JPG with quality 100 as you are doing shouldn't lower quality. Anyway, just in case I would go to the gallery and see if that image size is the same as the original

Also, is "recta" still initialized to imgPhoto.width and imgPhoto.height? If so, try to change it to bm.Width and bm.Height
Thanks for your time Jordi. Recta is initialized exactly as you said. I will keep looking why output lowers quality. If you find a solution please inform me. Maybe it's a bug from ADExtDrawing library, while implementing matrix filter. I dont know what else to think.... Btw thanks for dimensions info.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Well, you can do a quick test saving and loading the same bitmap multiple times. So you will confirm or discard where the problem is
 
Upvote 0
Top