B4J Question View cam into imageView client-server

Discussion in 'B4J Questions' started by klingon467, Jun 4, 2015.

  1. klingon467

    klingon467 Member Licensed User

    Hi, i make client (B4A) and Server (B4J)
    now into my client i have this
    (i use CameraExClass)
    Code:
    Public Sub Send(Image() As Byte) 'send data as byte
       If client.IsInitialized = False OR client.Connected = False Then
         Connect
       
    Else
         raf.WriteInt(
    Image.Length, 0)
         astreams.Write(buffer)
         manna(
    "CAM" & "SPLT"'send my string header for splitting
         astreams.Write(Image)'write image frame into streaming
        
       
    End If
    End Sub
    into server (B4J)

    Code:
    Case "CAM"
      
      
    Dim InputStream1 As InputStream
       InputStream1.InitializeFromBytesArray(Buffer, 
    0, Buffer.Length)
       
    Dim Bitmap1 As Image
       Bitmap1.Initialize2(InputStream1)
       imgv1.SetImage(Bitmap1)
    but not work! :-(
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. klingon467

    klingon467 Member Licensed User

    Hi Erel!,
    I saw your example... and I managed to save the screenshoot on sdcard....

    client ===> Connector
    Code:
    Sub AStreams_NewData (mbuffer() As Byte)'ricevo dati
    Try
        
    Dim msg As String
           msg = 
    BytesToString(mbuffer, 0, mbuffer.Length, "UTF8")
           
    Dim opt() As String
           opt = 
    Regex.Split("SPLT", msg)
          
           
    Select opt(0)
           
    Case "CHAT" 'send chat message
                  ToastMessageShow(opt(1), False)
           
    Case "enChat" 'enable form activity rChat
                StartActivity(chat)
            
    Case "enCam" 'enable form activity rCam
            StartActivity(rCam)
            
    Case "disCam" 'disable rCam
                  StartActivity(aClose)' not work
            Case "CamFront" 'enable front cam
            CallSubDelayed(rCam, "CameraChangeFront")
            
    Case "CamBack" 'enable back cam
            CallSubDelayed(rCam, "CameraChangeBack")
            
    Case "screen" 'take a picture
            CallSubDelayed(rCam, "screenshot")
            
    Case "enGeoloc"
            manna(
    "GEO" & "SPLT" & p.GetSettings("android_id") & "SPLT" & lat & "SPLT" & lon & "SPLT" & speed)
           
    End Select
                       
    Catch
        
    Log(LastException.Message)
        
    End Try
          
    End Sub
    client ====> rCam
    Code:
    Sub Camera1_Ready (Success As Boolean)
        
    If Success Then
            camEx.StartPreview
            
    Log("Supported sizes:")
            
    For Each size As CameraSize In camEx.GetSupportedPicturesSizes
                
    Log(size.Width & "x" & size.Height)
            
    Next
            camEx.SetJpegQuality(
    90)
            
    Log(camEx.GetSupportedFlashModes)
            camEx.CommitParameters
        
    Else
            
    ToastMessageShow("Cannot open camera."True)
        
    End If
    End Sub
    Sub Camera1_Preview (PreviewPic() As Byte)
            
    Try
            
    If DateTime.Now > lastPreviewSaved + IntervalMs Then
            
    Dim jpeg() As Byte = camEx.PreviewImageToJpeg(PreviewPic, 70)
            lastPreviewSaved = 
    DateTime.Now
            
    'CallSubDelayed2(Communicator, "Send", jpeg)
            CallSubDelayed2(astreamO, "WriteBitmap"jpeg)
        
    End If
                
    Catch
        
    Log(LastException.Message)
        
    End Try
    End Sub
    Sub Camera1_PictureTaken (Data() As Byte)
            
    Try
            
    'create a random jpeg
            camEx.SavePictureToFile(Data, File.DirRootExternal, "screen_" & Rnd(1999999) & ".jpeg")'qui salva uno screenshot nella sd
        camEx.StartPreview
                    
    Catch
        
    Log(LastException.Message)
        
    End Try
    End Sub
    Sub screenshot()
    camEx.TakePicture
    End Sub
    ok! but now for send my frame of cam i use AsyncStreamsObject class?
    Code:
    'Sends a bitmap.
    Public Sub WriteBitmap (Key As String, Value As Bitmap)
        
    Dim name(1As String
        
    Dim raf As RandomAccessFile = sharedWrite(Key, name)
        raf.WriteByte(TYPE_BITMAP, raf.CurrentPosition)
        sharedWriteComplete(raf, name(
    0))
        
    Dim secondPart As String = "~" & name(0)
        
    Dim out As OutputStream = File.OpenOutput(TempFolder, secondPart, False)
        Value.WriteToStream(out, 
    100"jpeg")
        out.Close
        astream.WriteStream(
    File.OpenInput(TempFolder, secondPart), _
            
    File.Size(TempFolder, secondPart))
    End Sub
    and into my server in listening how I receive it? and I put in imageview?

    I can postarti in private the full source?

    thanks
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. klingon467

    klingon467 Member Licensed User

    Hi Erel,
    i solved using 2 stream

    1 for strings with split and 1 for image.....

    Code:
    Sub Client_Connected (Successful As Boolean)
        tryingToConnect = 
    False
        
    Log("Client_Connected: " & Successful)
        
    If Successful = False Then
            
    Log(LastException)
            
    ToastMessageShow("Error connecting."True)
        
    Else
            astreams.InitializePrefix(client.InputStream, 
    False, client.OutputStream, "AStreams")
            manna(
    "CLIENT" & "SPLT" & p.GetSettings("android_id") & "SPLT" & p.Manufacturer & "SPLT" & _
            p.Model & 
    "SPLT" & p.Product & "SPLT" & p.GetDataState & "SPLT" & p.GetNetworkOperatorName & "SPLT" & _
            p.GetNetworkType & 
    "SPLT" & p.GetPhoneType & "SPLT" & p.GetSimOperator)
           
            
    'inizializzo secondo stream per inviare solo immagini-oggetti
        astreamsJ.Initialize(client.InputStream, client.OutputStream, "astreamsJ")

        
    End If
    End Sub
    work it! but now i can close only the stream for the cam?

    thanks
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    It is a mistake to use two AsyncStreams with a single connection. It will not work properly.
     
Loading...