Android Code Snippet Resize Bitmap with a maximum Byte limit

Discussion in 'Code Snippets' started by Star-Dust, Dec 2, 2017.

  1. Star-Dust

    Star-Dust Expert Licensed User

    Sometimes the images we use in our Apps are overly large. In some cases, images are entered by the user and therefore we do not know the size. And to manage them we need them to fall within a certain Byte limit.

    How to reduce them within the limit?

    Here is a code that allows you to redimension the images while maintaining the proportion and returning within the Byte mitie that serves us

    Code:
    Dim B As Bitmap = ResizeBitmapMaxByte(LoadBitmap(File.DirAssets,"test.png"),10240' 10 k

    ' Sub for resize
    Sub ResizeBitmapMaxByte(Original As Bitmap, MaxByte As Long) As Bitmap
        
    Dim ByteSize As Long
        
    Dim ObjectBitmap As JavaObject = Original
        
    Dim Api_Lelvel As JavaObject

        Api_Lelvel.InitializeStatic(
    "android.os.Build.VERSION")
        
    If Api_Lelvel.GetField("SDK_INT") >= 19 Then  'kitkat
            ByteSize = ObjectBitmap.RunMethod("getAllocationByteCount"Null)
        
    else if Api_Lelvel.GetField("SDK_INT") >= 12 Then   'Android 3.1 Honeycomb
            ByteSize = ObjectBitmap.RunMethod("getByteCount"Null)
        
    Else     'earlier Android versions
            ByteSize = ObjectBitmap.RunMethod("getRowBytes"Null) * Original.Height
        
    End If

        
    Dim Ratio As Float = Sqrt(MaxByte/ByteSize)

        
    If Ratio<1 Then
            
    Dim r As Reflector
            
    Dim b As Bitmap
            
    Dim Width As Int = (Original.Width * Ratio)
            
    Dim Height As Int = (Original.Height * Ratio)

            b = r.RunStaticMethod(
    "android.graphics.Bitmap""createScaledBitmap", _
                
    Array As Object(Original, Width , Height, False), _
                
    Array As String("android.graphics.Bitmap""java.lang.int""java.lang.int""java.lang.boolean"))
            
    Return b
        
    Else
            
    Return Original
        
    End If
    End Sub
     
    Last edited: Dec 2, 2017
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can also use Bitmap.ResizeBitmap or LoadBitmapResize.
     
  3. Star-Dust

    Star-Dust Expert Licensed User

    Of course, but this code mainly serves to reduce the image based on weight in bytes. The small size is calculated based on this. So Bitmap.ResizeBitmap could replace the second part of the code.

    This is the code using BitmapResize
    Code:
    Sub ResizeBitmapMaxByte(Original As Bitmap, MaxByte As Long) As Bitmap
        
    Dim ByteSize As Long
        
    Dim ObjectBitmap As JavaObject = Original
        
    Dim Api_Lelvel As JavaObject

        Api_Lelvel.InitializeStatic(
    "android.os.Build.VERSION")
        
    If Api_Lelvel.GetField("SDK_INT") >= 19 Then  'kitkat
            ByteSize = ObjectBitmap.RunMethod("getAllocationByteCount"Null)
        
    else if Api_Lelvel.GetField("SDK_INT") >= 12 Then   'Android 3.1 Honeycomb
            ByteSize = ObjectBitmap.RunMethod("getByteCount"Null)
        
    Else     'earlier Android versions
            ByteSize = ObjectBitmap.RunMethod("getRowBytes"Null) * Original.Height
        
    End If

        
    Dim Ratio As Float = Sqrt(MaxByte/ByteSize)

        
    If Ratio<1 Then
            
    Dim Width As Int = (Original.Width * Ratio)
            
    Dim Height As Int = (Original.Height * Ratio)

            
    Return Original.Bitmap.ResizeBitmap(width, height, true)
        
    ELse
            
    Return Original
        
    End If
    End Sub
    This code would only work with B47 +, the first code also works for previous versions
     
    Last edited: Aug 10, 2019
  4. moster67

    moster67 Expert Licensed User

    Haven't tried above code but I believe the correct method name is Bitmap.Resize() and not Bitmap.ResizeBitmap().
    Thus, the return of the bitmap in the last posted snippet above should be:
    Code:
    Return Original.Resize(Width, Height, True)
     
  5. Star-Dust

    Star-Dust Expert Licensed User

    I don't remember if any method has changed since it was written. But then the code had been tried
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice