Android Question OutOfMemory Camera

headingwest

Member
Licensed User
Longtime User
Hi All, I'm trying to measure the "R" "G" and "B" in a camera photo. Basic idea is to scroll through each pixel and add up each color.

This is crashing with OutOfMemory half way through 640x480 camera image. Any ideas?

B4X:
Sub Camera1_PictureTaken (Data() As Byte)
       
    Dim In As InputStream
    In.InitializeFromBytesArray(Data, 0, Data.Length)
   
    Dim bmp As Bitmap
   
    bmp.Initialize2(In)
    In.Close
   
    Dim x As Int, y As Int, z As Int
    Dim numPix As Long
    Dim PixelColor As Int
    numPix = bmp.Height * bmp.Width
    Dim res(4) As Int
    Dim ARGB(4) As Long
   
    For x = 0 To bmp.Width - 1
        For y = 0 To bmp.Height - 1
            PixelColor = bmp.GetPixel(x,y)
           
            res(0) = Bit.UnsignedShiftRight(Bit.And(PixelColor, 0xff000000), 24)
            res(1) = Bit.UnsignedShiftRight(Bit.And(PixelColor, 0xff0000), 16)
            res(2) = Bit.UnsignedShiftRight(Bit.And(PixelColor, 0xff00), 8)
            res(3) = Bit.And(PixelColor, 0xff)
    '        Log("ARGB = " & res(0) & "," & res(1) & "," & res(2) & "," & res(3))       
           
            For z = 0 To 3
                ARGB(z) = ARGB(z) + res(z)       
            Next
           
        Next
        Log("Line " & x & " / " & bmp.Height)
    Next
   
    For z = 0 To 3
        ARGB(z) = ARGB(z) / numPix       
    Next
End Sub
 

headingwest

Member
Licensed User
Longtime User
Hi Erel,

Log(bmp.Width & " x " & bmp.Height) = 640 x 480

I'm using a different PC today and the error is on this line:
B4X:
res(0) = Bit.UnsignedShiftRight(Bit.And(PixelColor, 0xff000000), 24)

It's processed 126 / 480 lines of the bitmap when it crashes. The error seems to have changed but always occurs at this point. Here's 2 different errors thrown up:

Line 126 / 480 / 15544800
Error occurred on line: 100 (Main)
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:588)
at libcore.io.IoBridge.recvfrom(IoBridge.java:552)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
at java.io.InputStream.read(InputStream.java:162)
at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:149)
at java.io.BufferedInputStream.read(BufferedInputStream.java:295)
at libcore.io.Streams.readFully(Streams.java:81)
at java.io.DataInputStream.readShort(DataInputStream.java:152)
at anywheresoftware.b4a.shell.ShellConnector.readObject(ShellConnector.java:262)
at anywheresoftware.b4a.shell.Shell.setField(Shell.java:647)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:897)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:839)
at b4a.example.cameraexclass._camera_picturetaken(cameraexclass.java:481)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$1.run(BA.java:293)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:189)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
at libcore.io.IoBridge.recvfrom(IoBridge.java:549)
... 31 more

Line 126 / 480 / 15544800
Error occurred on line: 100 (Main)
java.net.SocketException: Socket closed
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:189)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
at libcore.io.IoBridge.recvfrom(IoBridge.java:549)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
at java.io.InputStream.read(InputStream.java:162)
at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:149)
at java.io.BufferedInputStream.read(BufferedInputStream.java:295)
at libcore.io.Streams.readFully(Streams.java:81)
at java.io.DataInputStream.readShort(DataInputStream.java:152)
at anywheresoftware.b4a.shell.ShellConnector.readObject(ShellConnector.java:262)
at anywheresoftware.b4a.shell.Shell.setField(Shell.java:647)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:897)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:839)
at b4a.example.cameraexclass._camera_picturetaken(cameraexclass.java:481)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$1.run(BA.java:293)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 
Upvote 0
Top