Android Question GetPixel from image without saving it

semar

Active Member
Licensed User
Hi all,
would be possible to check the RGB of a pixel (using getPixel) from an image, without prior save it and load it again ?
More precisely, when I take a picture with the smartphone camera, how can I access the values of each single pixel from the image, withoud saving and loading it again as a bitmap ?

Thanks in advance
Sergio
 

semar

Active Member
Licensed User
I apologize, I try to be more precise.

As you see, I can check against a pixel (in this example at 320,320) with the function btnTestPixel.
The question is, can I check for a desired pixel of the image, without to have to save it and load it first ?
In other terms, can I access to the pixels of the taken image in someway, without having to save it ?

Here is the code:

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: False
    #ApplicationLabel: Camera example
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    Private frontCamera As Boolean = True
End Sub

Sub Globals
    Private Panel1 As Panel
    Private camEx As CameraExClass
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
    InitializeCamera
End Sub

Private Sub InitializeCamera
   
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        camEx.Initialize(Panel1, frontCamera, Me, "Camera1")
        frontCamera = camEx.Front
    Else
        ToastMessageShow("No permission!!!", True)
    End If
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If camEx.IsInitialized Then
        camEx.Release
    End If
End Sub

Sub Camera1_Ready (Success As Boolean)
    If Success Then
        camEx.SetJpegQuality(90)
        camEx.SetContinuousAutoFocus
        camEx.CommitParameters
        camEx.StartPreview
        Log(camEx.GetPreviewSize)
       
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

Sub btnTakePicture_Click
   
    camEx.SetPictureSize(640,480)
    'ToastMessageShow(ps.Width & "x" & ps.Height, False)
    camEx.CommitParameters
   
    camEx.TakePicture
End Sub

Sub btnFocus_Click
    camEx.FocusAndTakePicture
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
    Dim filename As String = "1.jpg"
    Dim dir As String = File.DirInternal
   
    camEx.SavePictureToFile(Data, dir, filename)
    camEx.StartPreview 'restart preview
    ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename), True)
    Log(dir & "\" & filename)
End Sub


Sub ChangeCamera_Click
    camEx.Release
    frontCamera = Not(frontCamera)
    InitializeCamera
End Sub

Sub btnEffect_Click
    Dim effects As List = camEx.GetSupportedColorEffects
    If effects.IsInitialized = False Then
        ToastMessageShow("Effects not supported.", False)
        Return
    End If
    Dim effect As String = effects.Get((effects.IndexOf(camEx.GetColorEffect) + 1) Mod effects.Size)
    camEx.SetColorEffect(effect)
    ToastMessageShow(effect, False)
    camEx.CommitParameters
End Sub

Sub btnFlash_Click
    Dim f() As Float = camEx.GetFocusDistances
    Log(f(0) & ", " & f(1) & ", " & f(2))
    Dim flashModes As List = camEx.GetSupportedFlashModes
    If flashModes.IsInitialized = False Then
        ToastMessageShow("Flash not supported.", False)
        Return
    End If
    Dim flash As String = flashModes.Get((flashModes.IndexOf(camEx.GetFlashMode) + 1) Mod flashModes.Size)
    camEx.SetFlashMode(flash)
    ToastMessageShow(flash, False)
    camEx.CommitParameters  
End Sub

Sub btnPictureSize_Click
    Dim pictureSizes() As CameraSize = camEx.GetSupportedPicturesSizes
    Dim current As CameraSize = camEx.GetPictureSize
    For i = 0 To pictureSizes.Length - 1
        If pictureSizes(i).Width = current.Width And pictureSizes(i).Height = current.Height Then Exit
    Next
    Dim ps As CameraSize = pictureSizes((i + 1) Mod pictureSizes.Length)
    camEx.SetPictureSize(ps.Width, ps.Height)
    ToastMessageShow(ps.Width & "x" & ps.Height, False)
    camEx.CommitParameters      
End Sub


Sub SeekBar1_ValueChanged (Value As Int, UserChanged As Boolean)
    If UserChanged = False Or camEx.IsZoomSupported = False Then Return
    camEx.Zoom = Value / 100 * camEx.GetMaxZoom
    camEx.CommitParameters
End Sub

Sub GetARGB(Color As Int) As Int()
    Private res(4) As Int
    res(0) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff000000), 24)
    res(1) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff0000), 16)
    res(2) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff00), 8)
    res(3) = Bit.And(Color, 0xff)
    Return res
End Sub

Sub btnTestPixel_Click
    Dim filename As String = "1.jpg"
    Dim dir As String = File.DirInternal
    Dim testBitmap As Bitmap = LoadBitmap(dir,filename)
   
    Dim pixel As Int = testBitmap.GetPixel(320,320)
   
   
    Private argb() As Int
    argb = GetARGB(pixel)
    Log("A = " & argb(0))
    Log("R = " & argb(1))
    Log("G = " & argb(2))
    Log("B = " & argb(3))
   
   
   
End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
B4X:
dim bmp as Bitmap
    dim inpstr as InputStream
    inpstr.InitializeFromBytesArray (Data, 0, data.length)
    bmp.Initialize2(inpstr)
    inpstr.Close


B4X:
Sub Camera1_PictureTaken (Data() As Byte)
    dim bmp as Bitemap
    dim inpstr as InputStream
    inpstr.InitializeFromBytesArray (Data, 0, data.length)
    bmp.Initialize2(inpstr)
    inpstr.Close
    
    Dim filename As String = "1.jpg"
    Dim dir As String = File.DirInternal
    
    camEx.SavePictureToFile(Data, dir, filename)
    camEx.StartPreview 'restart preview
    ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename), True)
    Log(dir & "\" & filename)
End Sub
 
Upvote 0
Top