Originally posted a B4J/inline Java version of this in the B4J Code Snippets (https://www.b4x.com/android/forum/t...byte-array-with-resize-quality-options.91746/). @Erel pointed out though that the XUI library had the functionality that I was doing with Java, so I decided to rewrite the routine. This routine uses 100% B4X basic syntax, no inline Java, not even any #If conditionals. Unlike the Java version, this version does not throw any exceptions (on purpose), but instead returns an empty byte array if it finds/encounters any issues (which are Log()'ed).
Example
Resource used:
Dim bImage As Image bImage.Initialize(File.DirAssets, "b.png") Dim c() As Byte = ImageToJPEGByteArray(bImage, -50, 0, 50) 'https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#content BytesToFile(File.DirApp, "b.jpg", c)
B4X:
' Converts image to JPEG byte array. Ability to resize and adjust JPEG quality.
' Negative width and height values = %, such that -50 = 50% and -200 = 200%
' Positive width and height values = pixel dimensions
' If one value (either width or height) are 0, then the other value is proportionally
' calculated from the first.
' If both width and height are 0 or -100, no resizing takes place
' If quality = -1, use default quality of 75
' Will return a zero length byte array if something is wrong with the parameters
Sub XUIImageToJPEGByteArray(aImage As Object, width As Int, height As Int, quality As Int) As Byte()
'Sanity check quality
If (quality < -1 And quality > 100) Then
Log("Parameter quality not in range (-1..100)")
Return Array As Byte ()
End If
If quality = -1 Then quality = 75
'Sanity check incoming image
Try
Dim image As B4XBitmap = aImage
Catch
Log(LastException)
Return Array As Byte ()
End Try
'Sanity check incoming image dimensions
Dim oldWidth As Int = image.Width
Dim oldHeight As Int = image.Height
If (oldWidth = 0 And oldHeight = 0 ) Then
Log("Source image with incorrect dimensions")
Return Array As Byte ()
End If
'See if were resizing the image
Dim resize As Boolean = True
If ((width = 0 And height = 0) Or (width = -100 Or height = -100)) Then resize = False
'Resize if necessary
If (resize) Then
Dim newWidth As Int = width
Dim newHeight As Int = height
'Calculate new dimensions
If (newWidth < 0) Then newWidth = -1 * oldWidth * newWidth / 100
If (newHeight < 0) Then newHeight = -1 * oldHeight * newHeight / 100
If (newWidth = 0) Then newWidth = oldWidth * newHeight / oldHeight
If (newHeight = 0 ) Then newHeight = oldHeight * newWidth / oldWidth
image = image.Resize(newWidth, newHeight, False)
End If
'Convert image to JPEG byte array
Dim out As OutputStream
out.InitializeToBytesArray(0)
image.WriteToStream(out, quality, "JPEG")
out.Close
'Done
Return out.ToBytesArray
End Sub
B4X:
Dim bImage As Image
bImage.Initialize(File.DirAssets, "b.png")
Dim c() As Byte = XUIImageToJPEGByteArray(bImage, -50, 0, 50)
'https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#content
BytesToFile(File.DirApp, "b.jpg", c)
Resource used:
Dim bImage As Image bImage.Initialize(File.DirAssets, "b.png") Dim c() As Byte = ImageToJPEGByteArray(bImage, -50, 0, 50) 'https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#content BytesToFile(File.DirApp, "b.jpg", c)
Last edited: