Android Question [Resolved] Unfortunately on Android 2.2

sangee

Member
Licensed User
Longtime User
Dear All..

Due to a unfortunate business idea I am forced to re write a camera application on Android 2.2. Its a severe problem as the CameraEx class does not work in this version :(.

I just need to capture frames from the camera in a fast timing (capture 2 images / second)

I tried to use the Camera1_PictureTaken method, it works but is very slow (captures only 1 image in two or three seconds)

I am not able to use the Camera1_Preview like in latest Androids as the PreviewImageToJpeg is not working properly as I simply dont know how to do it :D..The code I am trying to do is below..

The function PreviewImageToJpeg is to convert YUV format to JPEG which could be the problem I guess but I am not able to fix it.. need some help in this please..

Can any one help me in using the camera_preview with out using CameraExclass please...

Thanks,
Sangee


B4X:
Sub Camera1_Preview (Data() As Byte)
    Dim forDate As String
    DateTime.DateFormat ="HH.mm.ss.SS_dd-MM-yy"
    forDate=DateTime.Date(DateTime.now)
   
    Dim Tcr As String
    DateTime.DateFormat ="HH:mm:ss  dd-MM-yyyy"
    Tcr = DateTime.Date(DateTime.now)
   
    Dim tempstr As StringBuilder
    tempstr.Initialize
    tempstr.Append(forDate).Append("_").Append(Hostname).Append("_")
   
    InProg=True
        If DateTime.now > lastPreviewSaved + IntervalMs Then
            Dim jpeg() As Byte = PreviewImageToJpeg(Data, 90)
           
             Dim InputStream As InputStream
             InputStream.InitializeFromBytesArray(jpeg, 0, jpeg.Length)
            btmp.InitializeMutable(640,480)
            Canvas1.Initialize2(btmp)
            Dim Bitmap1 As Bitmap
             Bitmap1.Initialize2(InputStream)
             InputStream.Close

            Dim Rect1 As Rect
            Rect1.Initialize(0,0,btmp.Width ,btmp.Height)
            Canvas1.DrawRect(Rect1, Colors.Gray,True, 1dip)
            Canvas1.DrawBitmap(Bitmap1,Null,Rect1)
            Canvas1.DrawLine(btmp.Width -270,btmp.Height -20, btmp.Width -2, btmp.Height -20, Colors.Black , 16dip)
            Canvas1.DrawText(Hostname & " - " & Tcr,btmp.Width -270,btmp.Height -15,Typeface.DEFAULT,15,Colors.White,"LEFT")
            Dim out As OutputStream
            out = File.OpenOutput(File.DirRootExternal, "/Images/" & tempstr & ".jpg", False)
            Canvas1.Bitmap.WriteToStream(out,45,"JPEG")
            out.Close
       
            lastPreviewSaved = DateTime.now
        End If
        InProg=False

End Sub
 

sangee

Member
Licensed User
Longtime User
The PreviewImageToJpeg function..

B4X:
Public Sub PreviewImageToJpeg(data() As Byte, quality As Int) As Byte()

Try   
    Dim size, previewFormat As Object
    r.target = parameters
    size = r.RunMethod("getPreviewSize")
   
    previewFormat = r.RunMethod("getPreviewFormat")
    r.target = size
    Dim width = r.GetField("width"), height = r.GetField("height") As Int
    Dim width = r.GetField("width"), height = r.GetField("height") As Int
    Dim width = 640, height = 480 As Int
    'Log("width=" & width & " height=" & height)
    Dim yuvImage As Object = r.CreateObject2("android.graphics.YuvImage", _
        Array As Object(data, previewFormat, width, height, Null), _
        Array As String("[B", "java.lang.int", "java.lang.int", "java.lang.int", "[I"))
    r.target = yuvImage
    Dim rect1 As Rect
    rect1.Initialize(0, 0, r.RunMethod("getWidth"), r.RunMethod("getHeight"))
    Dim out As OutputStream
    out.InitializeToBytesArray(100)
    r.RunMethod4("compressToJpeg", Array As Object(rect1, quality, out), _
        Array As String("android.graphics.Rect", "java.lang.int", "java.io.OutputStream"))
    Return out.ToBytesArray
Catch
Log("EOEOEOEO" & LastException.Message)
    Return
End Try

End Sub

The error I get when the app starts..

B4X:
EOEOEOEOjava.lang.NoSuchMethodException: getPreviewSize


main_camera1_preview (java line: 335)


java.lang.NullPointerException
    at a3a.cam.update.main._camera1_preview(main.java:335)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.CameraW$2$1$1.onPreviewFrame(CameraW.java:147)
    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:338)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:884)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 
Upvote 0

sangee

Member
Licensed User
Longtime User
I have hard coded the size like below. Now I have the error while declaring the type of image.

I believe the method of declaring the preview format is wrong. Can you help me please..


B4X:
Public Sub PreviewImageToJpeg(data() As Byte, quality As Int) As Byte()

Try   
    Dim size, previewFormat As Object
    r.target = parameters
    'size = r.RunMethod("getPreviewSize")
   
    'previewFormat = r.RunMethod("getPreviewFormat")
    previewFormat = "YUV"
    'r.target = size
    'Dim width = r.GetField("width"), height = r.GetField("height") As Int
    'Dim width = r.GetField("width"), height = r.GetField("height") As Int
    Dim width = 640, height = 480 As Int
    'Log("width=" & width & " height=" & height)
    Dim yuvImage As Object = r.CreateObject2("android.graphics.YuvImage", _
        Array As Object(data, previewFormat, width, height, Null), _
        Array As String("[B", "java.lang.int", "java.lang.int", "java.lang.int", "[I"))
    r.target = yuvImage
    Dim rect1 As Rect
    'rect1.Initialize(0, 0, r.RunMethod("getWidth"), r.RunMethod("getHeight"))
    rect1.Initialize(0, 0, width, height)
    Dim out As OutputStream
    out.InitializeToBytesArray(100)
    r.RunMethod4("compressToJpeg", Array As Object(rect1, quality, out), _
        Array As String("android.graphics.Rect", "java.lang.int", "java.io.OutputStream"))
    Return out.ToBytesArray
Catch
Log("EOEOEOEO" & LastException.Message)
    Return
End Try

B4X:
EOEOEOEOjava.lang.IllegalArgumentException: argument type mismatch


main_camera1_preview (java line: 335)


java.lang.NullPointerException
    at a3a.cam.update.main._camera1_preview(main.java:335)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.CameraW$2$1$1.onPreviewFrame(CameraW.java:147)
    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:338)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:884)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 
Upvote 0

sangee

Member
Licensed User
Longtime User
As suggested.. I removed the Try / Catch block and the error is like below..

B4X:
main_previewimagetojpeg (java line: 738)


java.lang.RuntimeException: java.lang.IllegalArgumentException: argument type mismatch
    at anywheresoftware.b4a.agraham.reflection.Reflection.CreateObject2(Reflection.java:268)
    at a3a.cam.update.main._previewimagetojpeg(main.java:738)
    at a3a.cam.update.main._camera1_preview(main.java:331)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.CameraW$2$1$1.onPreviewFrame(CameraW.java:147)
    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:338)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)


    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:884)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
    at anywheresoftware.b4a.agraham.reflection.Reflection.CreateObject2(Reflection.java:265)
    ... 17 more
java.lang.RuntimeException: java.lang.IllegalArgumentException: argument type mismatch
 
Upvote 0

sangee

Member
Licensed User
Longtime User
Modified the function like below.. looks like the method is not available..

B4X:
Public Sub PreviewImageToJpeg(data() As Byte, quality As Int) As Byte()

'Try   
    Dim size, previewFormat As Object
    r.target = parameters
    'size = r.RunMethod("getPreviewSize")
    previewFormat = r.RunMethod("getPreviewFormat")
    'previewFormat = "YUV_420_888"
    'r.target = size
    'Dim width = r.GetField("width"), height = r.GetField("height") As Int
    'Dim width = r.GetField("width"), height = r.GetField("height") As Int
    Dim width = 640, height = 480 As Int
    'Log("width=" & width & " height=" & height)
    Dim yuvImage As Object = r.CreateObject2("android.graphics.YuvImage", _
        Array As Object(data, previewFormat, width, height, Null), _
        Array As String("[B", "java.lang.int", "java.lang.int", "java.lang.int", "[I"))
    r.target = yuvImage
    Dim rect1 As Rect
    'rect1.Initialize(0, 0, r.RunMethod("getWidth"), r.RunMethod("getHeight"))
    rect1.Initialize(0, 0, width, height)
    Dim out As OutputStream
    out.InitializeToBytesArray(100)
    r.RunMethod4("compressToJpeg", Array As Object(rect1, quality, out), _
        Array As String("android.graphics.Rect", "java.lang.int", "java.io.OutputStream"))
    Return out.ToBytesArray
'Catch
'Log("EOEOEOEO" & LastException.Message)
'    Return
'End Try

End Sub

Error ....


B4X:
main_previewimagetojpeg (java line: 733)


java.lang.NoSuchMethodException: getPreviewFormat


    at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
    at java.lang.Class.getDeclaredMethod(Class.java:748)
    at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod(Reflection.java:802)
    at a3a.cam.update.main._previewimagetojpeg(main.java:733)
    at a3a.cam.update.main._camera1_preview(main.java:331)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.CameraW$2$1$1.onPreviewFrame(CameraW.java:147)
    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:338)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)


    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:884)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NoSuchMethodException: getPreviewFormat
 
Upvote 0

sangee

Member
Licensed User
Longtime User
It works ! Now I am able to save images from the Camera1_Preview sub directly. Thanks for being there Erel.. :)

B4X:
Public Sub PreviewImageToJpeg(data() As Byte, quality As Int) As Byte()

Try   
    Dim size, previewFormat As Object
    r.target = parameters
    previewFormat = 17
    Dim width = 640, height = 480 As Int
    Dim yuvImage As Object = r.CreateObject2("android.graphics.YuvImage", _
    Array As Object(data, previewFormat, width, height, Null), _
    Array As String("[B", "java.lang.int", "java.lang.int", "java.lang.int", "[I"))
    r.target = yuvImage
    Dim rect1 As Rect
    rect1.Initialize(0, 0, width, height)
    Dim out As OutputStream
    out.InitializeToBytesArray(100)
    r.RunMethod4("compressToJpeg", Array As Object(rect1, quality, out), _
        Array As String("android.graphics.Rect", "java.lang.int", "java.io.OutputStream"))
    Return out.ToBytesArray
Catch
Log("EOEOEOEO" & LastException.Message)
    Return
End Try

End Sub
 
Upvote 0
Top