Resizing a image

aloof

Member
Licensed User
Longtime User
Hi

I'm looking at making a app to resize images.... ive looked at some samples of bitmap.
i think it is a little advanced for me i dont understand the steps involved... if someone can explain it... ie load file, create new file, define size or whatever it may be

thanks
 

Djembefola

Active Member
Licensed User
Longtime User
1. Loading a Bitmap:

B4X:
Dim bmp As Bitmap
bmp.Initialize(myDirectory, myFile)

2. Scaling a Bitmap (Reflection Library needed):

B4X:
Sub CreateScaledBitmap(Original As Bitmap, NewWidth As Int, NewHeight As Int) As Bitmap
    Dim r As Reflector
    Dim b As Bitmap
   
   
    b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
        Array As Object(Original, NewWidth, NewHeight, true), _
        Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
    Return b
End Sub

3. Saving a bitmap:

B4X:
Sub SaveBitmap (bmp As Bitmap, MyDir As String, MyFile As String)
   Dim out As OutputStream
   out=File.OpenOutput(MyDir,MyFile, False)
   bmp.WriteToStream(out,100,"PNG")
   out.close
End Sub
 
Last edited:
Upvote 0

aloof

Member
Licensed User
Longtime User
thanks so much for that, appreciated... i think i will pick that up easy


sorry about wrong forum
 
Upvote 0

aloof

Member
Licensed User
Longtime User
i tried using most of your code but couldnt get it working... can you see something i dont? thanks

Sub CreateScaledBitmap(bmp As Bitmap, NewWidth As Int, NewHeight As Int) As Bitmap
Dim r As Reflector
Dim b As Bitmap


b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
Array As Object(bmp, NewWidth, NewHeight, True), _
Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
Return b
End Sub

Sub SaveBitmap (bmp As Bitmap, dir As String, filename As String)
Dim out As OutputStream
out=File.OpenOutput(File.DirRootExternal, "reduced/" & "-Lower2.jpeg", False)
bmp.WriteToStream(out,5,"JPEG")
out.close
End Sub

Sub buttongo_click
Dim dir As String
Dim filename As String
Dim original As Bitmap
Dim bmp As Bitmap
bmp.Initialize(File.DirRootExternal, "reduced/" & "-Lower.jpeg")

CreateScaledBitmap(bmp,300,400)
SaveBitmap(bmp,File.DirRootExternal, "reduced/" & "-Lower.jpeg")

End Sub
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
B4X:
"reduced/" & "-Lower.jpeg"

i suppose, this is not a valid filename.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
this line in your code:

B4X:
CreateScaledBitmap(bmp,300,400)

should be

B4X:
bmp = CreateScaledBitmap(bmp,300,400)
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
Hello,

I noticed:

B4X:
bmp.WriteToStream(out,5,"JPEG")

also that line of code is really low resolution
I think using 80 gives you decent resolution and low files size

bmp.WriteToStream(out,80,"JPEG")

Best...
 
Last edited:
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Djembefola,

Sorry, I haven't checked the Java classes, but would it be possible to apply this:

B4X:
Sub CreateScaledBitmap(Original As Bitmap, NewWidth As Int, NewHeight As Int) As Bitmap
    Dim r As Reflector
    Dim b As Bitmap
    b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
        Array As Object(Original, NewWidth, NewHeight, true), _
        Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
    Return b
End Sub

modified for inverting and/or gray-scaling images?
Using agraham's reflector this seems to be a formidable approach to
image processing without requiring lots of code.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
Hi Djembefola,

Sorry, I haven't checked the Java classes, but would it be possible to apply this...modified for inverting and/or gray-scaling images?
Using agraham's reflector this seems to be a formidable approach to
image processing without requiring lots of code.

Hi Alfcen,
I know nothing about image processing, but i suppose, in most cases there are several objects/methods involved in applying image effects. The reflection library is fine, as long as you need just a single function call.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Thanks for you time. I will dig deeper into available objects and methods.
Cheers
Robert
 
Upvote 0

silvercover

Member
Licensed User
Longtime User
Resize and rotate images using handles

Hi,

How can I let users rotate and re-size loaded images on screen using handles around images corners?

attachment.php


Something like the times when you work with images in image editing software that let you resize and rotate images by mouse.

Thanks.
 

Attachments

  • handles.jpg
    handles.jpg
    47.3 KB · Views: 1,201
Upvote 0
Top