Android Question How to convert this java code to B4a?

Edson Freire

Member
Licensed User
Longtime User
The code is:
B4X:
    public static byte[] getBytesFromBitMap(Bitmap bitmap, int mode) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width*height];
        if(mode == 0 || mode == 1){
            byte[] res = new byte[width*height/8 + 5*height/8];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for(int i = 0; i < height/8; i++){
                res[0 + i*(width+5)] = 0x1b;
                res[1 + i*(width+5)] = 0x2a;
                res[2 + i*(width+5)] = (byte) mode;
                res[3 + i*(width+5)] = (byte) (width%256);
                res[4 + i*(width+5)] = (byte) (width/256);
                for(int j = 0; j < width; j++){
                    byte gray = 0;
                    for(int m = 0; m < 8; m++){
                        int clr = pixels[j + width*(i*8+m)];
                        int red = (clr & 0x00ff0000) >> 16;
                        int green = (clr & 0x0000ff00) >> 8;
                        int blue = clr & 0x000000ff;
                        gray = (byte) ((RGB2Gray(red, green, blue)<<(7-m))|gray);
                    }
                    res[5 + j + i*(width+5)] = gray;
                }
            }
            return res;
        }else if(mode == 32 || mode == 33){
            byte[] res = new byte[width*height/8 + 5*height/24];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for(int i = 0; i < height/24; i++){
                res[0 + i*(width*3+5)] = 0x1b;
                res[1 + i*(width*3+5)] = 0x2a;
                res[2 + i*(width*3+5)] = (byte) mode;
                res[3 + i*(width*3+5)] = (byte) (width%256);
                res[4 + i*(width*3+5)] = (byte) (width/256);
                for(int j = 0; j < width; j++){
                    for(int n = 0; n < 3; n++){
                        byte gray = 0;
                        for(int m = 0; m < 8; m++){
                            int clr = pixels[j + width*(i*24 + m + n*8)];
                            int red = (clr & 0x00ff0000) >> 16;
                            int green = (clr & 0x0000ff00) >> 8;
                            int blue = clr & 0x000000ff;
                            gray = (byte) ((RGB2Gray(red, green, blue)<<(7-m))|gray);
                        }
                        res[5 + j*3 + i*(width*3+5) + n] = gray;
                    }
                }
            }
            return res;
        }else{
            return new byte[]{0x0A};
        }

    }



    private static byte RGB2Gray(int r, int g, int b) {
        return (false ? ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) > 200)
                : ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) < 200)) ? (byte) 1 : (byte) 0;
    }
 

Peter Simpson

Expert
Licensed User
Longtime User
Upvote 0

asales

Expert
Licensed User
Longtime User
What do you want do with this code?
Maybe there is a class or library in B4A that make what you want to do.
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
Hi,

try this :

B4X:
#Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

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
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private NativeMe As JavaObject
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    If FirstTime Then
        NativeMe.InitializeContext
    End If   

    Dim bmp As Bitmap
    bmp.InitializeMutable(100, 100)
   
    Dim arrData() As Byte
    arrData = NativeMe.RunMethod("getBytesFromBitMap", Array As Object(bmp, 1))
   
    Log(arrData.Length)
       
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#If Java

import android.graphics.Bitmap;

public static byte[] getBytesFromBitMap(Bitmap bitmap, int mode) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width*height];
        if(mode == 0 || mode == 1){
            byte[] res = new byte[width*height/8 + 5*height/8];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for(int i = 0; i < height/8; i++){
                res[0 + i*(width+5)] = 0x1b;
                res[1 + i*(width+5)] = 0x2a;
                res[2 + i*(width+5)] = (byte) mode;
                res[3 + i*(width+5)] = (byte) (width%256);
                res[4 + i*(width+5)] = (byte) (width/256);
                for(int j = 0; j < width; j++){
                    byte gray = 0;
                    for(int m = 0; m < 8; m++){
                        int clr = pixels[j + width*(i*8+m)];
                        int red = (clr & 0x00ff0000) >> 16;
                        int green = (clr & 0x0000ff00) >> 8;
                        int blue = clr & 0x000000ff;
                        gray = (byte) ((RGB2Gray(red, green, blue)<<(7-m))|gray);
                    }
                    res[5 + j + i*(width+5)] = gray;
                }
            }
            return res;
        }else if(mode == 32 || mode == 33){
            byte[] res = new byte[width*height/8 + 5*height/24];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for(int i = 0; i < height/24; i++){
                res[0 + i*(width*3+5)] = 0x1b;
                res[1 + i*(width*3+5)] = 0x2a;
                res[2 + i*(width*3+5)] = (byte) mode;
                res[3 + i*(width*3+5)] = (byte) (width%256);
                res[4 + i*(width*3+5)] = (byte) (width/256);
                for(int j = 0; j < width; j++){
                    for(int n = 0; n < 3; n++){
                        byte gray = 0;
                        for(int m = 0; m < 8; m++){
                            int clr = pixels[j + width*(i*24 + m + n*8)];
                            int red = (clr & 0x00ff0000) >> 16;
                            int green = (clr & 0x0000ff00) >> 8;
                            int blue = clr & 0x000000ff;
                            gray = (byte) ((RGB2Gray(red, green, blue)<<(7-m))|gray);
                        }
                        res[5 + j*3 + i*(width*3+5) + n] = gray;
                    }
                }
            }
            return res;
        }else{
            return new byte[]{0x0A};
        }

    }



    private static byte RGB2Gray(int r, int g, int b) {
        return (false ? ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) > 200)
                : ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) < 200)) ? (byte) 1 : (byte) 0;
    }
#End If
 
Upvote 0

Edson Freire

Member
Licensed User
Longtime User
Hi,

try this :

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private NativeMe As JavaObject
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    If FirstTime Then
        NativeMe.InitializeContext
    End If  

    Dim bmp As Bitmap
    bmp.InitializeMutable(100, 100)
  
    Dim arrData() As Byte
    arrData = NativeMe.RunMethod("getBytesFromBitMap", Array As Object(bmp, 1))
  
    Log(arrData.Length)
      
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#If Java

import android.graphics.Bitmap;

public static byte[] getBytesFromBitMap(Bitmap bitmap, int mode) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width*height];
        if(mode == 0 || mode == 1){
            byte[] res = new byte[width*height/8 + 5*height/8];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for(int i = 0; i < height/8; i++){
                res[0 + i*(width+5)] = 0x1b;
                res[1 + i*(width+5)] = 0x2a;
                res[2 + i*(width+5)] = (byte) mode;
                res[3 + i*(width+5)] = (byte) (width%256);
                res[4 + i*(width+5)] = (byte) (width/256);
                for(int j = 0; j < width; j++){
                    byte gray = 0;
                    for(int m = 0; m < 8; m++){
                        int clr = pixels[j + width*(i*8+m)];
                        int red = (clr & 0x00ff0000) >> 16;
                        int green = (clr & 0x0000ff00) >> 8;
                        int blue = clr & 0x000000ff;
                        gray = (byte) ((RGB2Gray(red, green, blue)<<(7-m))|gray);
                    }
                    res[5 + j + i*(width+5)] = gray;
                }
            }
            return res;
        }else if(mode == 32 || mode == 33){
            byte[] res = new byte[width*height/8 + 5*height/24];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for(int i = 0; i < height/24; i++){
                res[0 + i*(width*3+5)] = 0x1b;
                res[1 + i*(width*3+5)] = 0x2a;
                res[2 + i*(width*3+5)] = (byte) mode;
                res[3 + i*(width*3+5)] = (byte) (width%256);
                res[4 + i*(width*3+5)] = (byte) (width/256);
                for(int j = 0; j < width; j++){
                    for(int n = 0; n < 3; n++){
                        byte gray = 0;
                        for(int m = 0; m < 8; m++){
                            int clr = pixels[j + width*(i*24 + m + n*8)];
                            int red = (clr & 0x00ff0000) >> 16;
                            int green = (clr & 0x0000ff00) >> 8;
                            int blue = clr & 0x000000ff;
                            gray = (byte) ((RGB2Gray(red, green, blue)<<(7-m))|gray);
                        }
                        res[5 + j*3 + i*(width*3+5) + n] = gray;
                    }
                }
            }
            return res;
        }else{
            return new byte[]{0x0A};
        }

    }



    private static byte RGB2Gray(int r, int g, int b) {
        return (false ? ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) > 200)
                : ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) < 200)) ? (byte) 1 : (byte) 0;
    }
#End If
Thanks!
 
Upvote 0
Top