how to resize image just after creating it?

ozgureffe

Member
Licensed User
Longtime User
Hi,
My problem is
I'm creating an image file from canvas then i want to resize it because canvas size is very large.

But while trying to manage that i am getting an error about FileNotFoundException.

Maybe i am calling ResizeBitmap sub before creating image file


B4X:
Sub btn_save_Click
    Dim Out As OutputStream
    Bitmap1.Initialize3(Canvas1.Bitmap)
    fileName = DateTime.Now & ".PNG"
    Out = File.OpenOutput(File.DirRootExternal & "/fingerNotes",fileName, False)
    Bitmap1.WriteToStream(out, 10, "PNG")
    Out.Close
    
    ResizeBitmap(LoadBitmap(File.DirAssets,fileName), 100, 20)
    
End Sub

Sub ResizeBitmap(original As Bitmap, width As Int, height As Int) As Bitmap
    Dim new As Bitmap
    new.InitializeMutable(width, height)
    Dim c As Canvas
    c.Initialize2(new)
    Dim destRect As Rect
    destRect.Initialize(0, 0, width, height)
    c.DrawBitmap(original, Null, destRect)
    Return new
End Sub

It is also OK for me to resize image file before saving it... Is it possible?

Thank you...
 

Djembefola

Active Member
Licensed User
Longtime User
You're writing to

B4X:
File.DirRootExternal

but you try to load the File from

B4X:
File.DirAssets
 
Upvote 0

ozgureffe

Member
Licensed User
Longtime User
Sorry TYPO corrected.

B4X:
Sub btn_save_Click
    Dim Out As OutputStream
    Bitmap1.Initialize3(Canvas1.Bitmap)
    fileName = DateTime.Now & ".PNG"
    Out = File.OpenOutput(File.DirRootExternal & "/fingerNotes",fileName, False)
    Bitmap1.WriteToStream(out, 10, "PNG")
    Out.Close
    
    ResizeBitmap(LoadBitmap(File.DirRootExternal & "/fingerNotes",fileName), 100, 20)
    
End Sub

Sub ResizeBitmap(original As Bitmap, width As Int, height As Int) As Bitmap
    Dim new As Bitmap
    new.InitializeMutable(width, height)
    Dim c As Canvas
    c.Initialize2(new)
    Dim destRect As Rect
    destRect.Initialize(0, 0, width, height)
    c.DrawBitmap(original, Null, destRect)
    Return new
End Sub

But this time it is not resizing.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I do not grasp what you are trying to achieve, but what you are doing is:

When btn_save is clicked:

You save the bitmap that is on canvas1

You then call ResizeBitmap using the saved file

ResizeBitmap returns a resized bitmap that you are not using or saving

I suspect that you think that ResizeBitmap will automatically save the new bitmap, it won't

To resize the bitmap and save the resized image:

B4X:
Sub btn_save_Click
    Dim Out As OutputStream
    Dim Smaller as Bitmap
    Bitmap1.Initialize3(Canvas1.Bitmap)
    Smaller = ResizeBitmap(Bitmap1, 100, 20)
    fileName = DateTime.Now & ".PNG"
    Out = File.OpenOutput(File.DirRootExternal & "/fingerNotes",fileName, False)
    Smaller.WriteToStream(out, 10, "PNG")
    Out.Close
 
End Sub

Sub ResizeBitmap(original As Bitmap, width As Int, height As Int) As Bitmap
    Dim new As Bitmap
    new.InitializeMutable(width, height)
    Dim c As Canvas
    c.Initialize2(new)
    Dim destRect As Rect
    destRect.Initialize(0, 0, width, height)
    c.DrawBitmap(original, Null, destRect)
    Return new
End Sub

NOTE: I have not tested the ResizeBitmap code, I assume it works!
 
Upvote 0

Yeshua

Member
Licensed User
Longtime User
resize picture after taken.

:sign0085:


I'm using this lib: ACL (version 4.60), Phone (vesion 1.75), Core (version 1.90)
this is my project

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

Dim VerticalScreen As Phone

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Dim PanelCamera As Panel
Dim CounterY As Int
Dim CameraY As AdvancedCamera

End Sub

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("photo")
VerticalScreen.SetScreenOrientation(1)

End Sub

Sub CameraY_Ready(Succes As Boolean)

If Succes Then
CameraY.OriPortrait
CameraY.StartPreview
End If

End Sub

Sub Activity_Pause (UserClosed As Boolean)

CameraY.StopPreview
CameraY.Release

End Sub

Sub Activity_Resume

CameraY.Initialize(PanelCamera,"CameraY")

End Sub

Sub CameraY_PictureTaken2(Data() As Byte)

CameraY.StartPreview
Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, "Image" & CounterY & ".bmp", False)
out.WriteBytes(Data, 0, Data.Length)
out.Close
ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "Image" & CounterY & ".bmp"), True)

End Sub

Sub take_Click

CameraY.TakePicture2(90)
CounterY = CounterY + 1

End Sub

Sub exit_Click

ExitApplication

End Sub
 
Upvote 0
Top