Problem with size when saving bitmap

mrossen

Active Member
Licensed User
Longtime User
Hi,

I am loading a 645px / 1011px / 300 dpi with at printable size as 5,45x8,56cm image as a imageview.

When I save it again with Canvas1.Bitmap.WriteToStream(out, 100, "PNG") the size has changed to 967px / 1516px / 72 dpi with at printable size as 34,11x53,48cm

When I there after resize the image to 5,45x8,56cm the quality is very poor.

Is there a way to control the image dpi?

I need better resolution than 72dpi because I need to print the image

Mogens

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

End Sub

Sub Globals
   
   Dim ImageViewPinCard As ImageView 
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("main")
   
   Dim Canvas1 As Canvas
   Dim Bitmap1 As Bitmap
   Dim smaller As Bitmap
      
   Canvas1.Initialize(ImageViewPinCard) 'this canvas will draw on the activity background
   Canvas1.DrawTextRotated("Pincode: 1234567", 150dip, 250dip, Typeface.DEFAULT_BOLD, 18, Colors.Black, "CENTER", -90)
   Bitmap1.Initialize3(Canvas1.Bitmap)
   'Smaller = ResizeBitmap(Bitmap1, 210, 330)
   
   Dim Out As OutputStream
   Out = File.OpenOutput(File.DirRootExternal, "Test.png", False)
   'smaller.WriteToStream(out, 100, "PNG")
   Canvas1.Bitmap.WriteToStream(out, 100, "PNG")
   Out.Close

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

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
 

mrossen

Active Member
Licensed User
Longtime User
Here you are. I have made the original picture smaller. Otherwise I could not upload it because of its filesize.

But the original picture is still 300dpi an when saved from android 72dpi

Mogens
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not sure if it is possible to change the saved image density.
You can try to use the following method to set the bitmap density (requires Reflection library):
B4X:
Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("main")
   
   Dim Canvas1 As Canvas
   Dim Bitmap1 As Bitmap
   Dim smaller As Bitmap
   Bitmap1.InitializeMutable(100dip, 100dip)
   SetDensity(Bitmap1, 300)
   Canvas1.Initialize2(Bitmap1) 'this canvas will draw on the activity background
   Canvas1.DrawTextRotated("Pincode: 1234567", 150dip, 250dip, Typeface.DEFAULT_BOLD, 18, Colors.Black, "CENTER", -90)
   
   Dim Out As OutputStream
   Out = File.OpenOutput(File.DirRootExternal, "Test.png", False)
   'smaller.WriteToStream(out, 100, "PNG")
   Canvas1.Bitmap.WriteToStream(Out, 100, "PNG")
   Out.Close

End Sub
Sub SetDensity(Bmp As Bitmap, BitmapDensity As Int)
   Dim r As Reflector
   r.Target = Bmp
   r.RunMethod2("setDensity", BitmapDensity, "java.lang.int")
End Sub
 
Upvote 0
Top