B4J Question Obtain Image height and width

AHilton

Active Member
Licensed User
I'm finding that loading pictures (jpg mostly) like ...

dim im as image
im.initialize(SourceDir, PicFilename)

... Doesn't always give me valid im.height or im.width. Sometimes, those are null values. The pictures themselves seem to be fine as they can be viewed by several different standard viewers.

Ultimately, I'm creating thumbnails with these images being pulled from an email server. It works for the majority of pictures, but not all.

Any ideas on why some pictures (again, seemingly valid pictures otherwise) give null values when loaded into an Image?

Do I have alternatives to get the heights and widths for those pictures?
 

AHilton

Active Member
Licensed User
Here are 2 links. One to a "good" picture where the im.Height and im.Width return a value and one a "bad" picture where everything is "null" within im.

https://drive.google.com/file/d/0B0oh6QknY6i-djJ0SHFtcFBRRjQ/view?usp=sharing

https://drive.google.com/file/d/0B0oh6QknY6i-aDY5OXA2RFNnNzA/view?usp=sharing

Here's the code where I save the original (large) picture from its' temp email download to the final directory and where I create a thumbnail and save it to its' own directory ...

B4X:
Sub SavePicture (SourceDir As String, PicFilename As String, SaveDir As String)

    Try
        ' Copy the whole image to final dir
        File.Copy(SourceDir, PicFilename, SaveDir, PicFilename)
        ' Make a thumbnail and save it to the \thumbs dir
        Dim im As Image
        Dim can As Canvas
        im.Initialize(SourceDir,PicFilename)
        Dim jo As JavaObject
        can.Initialize("")
        jo = can

        ' Determine what the height setting should be based on the original size
        Dim SetWidth As Double = 150.0
        Dim SetHeight As Double
        If im.width < 1 Then                ' Some pictures will have null values in everything!
            SetHeight = SetWidth
        Else
            Dim SetHeight As Double = (SetWidth / im.Width) * im.Height
        End If
        jo.RunMethod("setWidth",Array(SetWidth))
        jo.RunMethod("setHeight",Array(SetHeight))
        can.DrawImage(im,0.0,0.0,SetWidth,SetHeight)
        Dim out As OutputStream = File.OpenOutput(SaveDir & "\thumbs", PicFilename, True)
        can.snapshot.WriteToStream(out)       
        out.Close
    Catch
        Main.LogMessage("Error Saving Picture" & LastException)
    End Try
End Sub
 

AHilton

Active Member
Licensed User
BeneBarros,

Your class works to give me a height and width to all of the pictures. Thanks!
However, it's not solving the ultimate problem because the saved thumbnail pictures aren't valid image files. They are very small (under 1k file size) when compared to the thumbnail pictures that DO work (ranging around 50k in file size). So, something else is going wrong. Possibly the Image or Canvas objects? Anyway, I'll create a new thread about that.
 

StarinschiAndrei

Active Member
Licensed User
BeneBarros,

Your class works to give me a height and width to all of the pictures. Thanks!
However, it's not solving the ultimate problem because the saved thumbnail pictures aren't valid image files. They are very small (under 1k file size) when compared to the thumbnail pictures that DO work (ranging around 50k in file size). So, something else is going wrong. Possibly the Image or Canvas objects? Anyway, I'll create a new thread about that.
Also you can try this :
B4X:
Dim bmp As Image
     bmp.Initialize(file.DirAssets,"test.png")
OR
       bmp.Initialize2(inputstream1)
       Log(bmp.Height&" X "&bmp.Width)
 
Top