B4J Question [SOLVED] Retrieve image from SQLite

jroriz

Active Member
Licensed User
Longtime User
I am getting the error below on Read sub.

How to acomplish this?

Log:
CreateTable: CREATE TABLE IF NOT EXISTS [tids] ([id] BLOB PRIMARY KEY, [name] TEXT)
Records: 4
(IllegalArgumentException) java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0

Whole code:

B4X:
#Region Project Attributes
    #MainFormWidth: 200
    #MainFormHeight: 200

    #AdditionalJar: sqlite-jdbc-3.7.2


#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private SQL1 As SQL
    Private c3po As AWTRobot
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    
    SQL1.InitializeSQLite(File.DirApp, "Dados.db", True)

    CreateDatabase
    Store
    Read
End Sub

Sub Read
    Dim Cursor1 As ResultSet
    Cursor1 = SQL1.ExecQuery("SELECT id FROM tids LIMIT 1")

    Dim Buffer() As Byte 'declare an empty byte array
    Buffer = Cursor1.GetBlob("id")
 
    Dim Bitmap1 As B4XBitmap = BytesToImage(Buffer)
    
    StoreJPEG(Bitmap1, DateTime.Now)
End Sub

Sub StoreJPEG(bitmap As B4XBitmap, Name As String)
    Try
        Dim out As OutputStream
        out = File.OpenOutput(File.DirApp, Name & ".JPEG", False)
        bitmap.WriteToStream(out, 100, "JPEG")
        out.Close
    Catch
        Log(LastException)
    End Try
End Sub

Sub Store
    
    Dim bmp1 As B4XBitmap = Capture(Rnd(1,200),Rnd(1,200),Rnd(1,200),Rnd(1,200))
    Dim BC As BitmapCreator
    BC.Initialize(bmp1.Width,bmp1.Height)
    BC.CopyPixelsFromBitmap(bmp1)

    SQL1.ExecNonQuery2($"INSERT INTO tids (id, name) VALUES (?, '${DateTime.Now}')"$, Array As Object(BC.Buffer))

    ' just for show that the record was stored correctly
    Dim Records As Int
    Records = SQL1.ExecQuerySingleResult("SELECT count(*) FROM tids")
    Log("Records: " & Records)
End Sub

Sub CreateDatabase
    Dim m As Map
    m.Initialize
    m.Put("id", DBUtils.DB_BLOB)
    m.Put("name", DBUtils.DB_TEXT)
    DBUtils.CreateTable(SQL1, "tids", m, "id")
End Sub

Sub Capture(x As Int, y As Int, w As Int, h As Int) As B4XBitmap
    c3po.ScreenCurrentRectangleSetAsArbitrary(x, y, w, h)
    Return BytesToImage(c3po.ScreenCaptureAsByteArray)
End Sub

Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)

    Dim bmp As Image
    bmp.Initialize2(In)

    Return bmp
End Sub


'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
 

XbNnX_507

Active Member
Licensed User
Longtime User
B4X:
Sub Read
    Dim Cursor1 As ResultSet
    Cursor1 = SQL1.ExecQuery("SELECT id FROM tids LIMIT 1")

    Dim Buffer() As Byte 'declare an empty byte array

    Do While Cursor1.NextRow
        Buffer = Cursor1.GetBlob("id")
    Loop
 
    Dim Bitmap1 As B4XBitmap = BytesToImage(Buffer)
    
    StoreJPEG(Bitmap1, DateTime.Now)
End Sub
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
The error persists ...

Waiting for debugger to connect...
Program started.
Error occurred on line: 50 (Main)
java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:324)
at anywheresoftware.b4a.objects.B4XViewWrapper$B4XBitmapWrapper.WriteToStream(B4XViewWrapper.java:698)
at b4j.example.main._storejpeg(main.java:224)
at b4j.example.main._read(main.java:135)
at b4j.example.main._appstart(main.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:627)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:236)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
check this part if the bitmap have it size, use a breakpoint in debug mode to stop there and watch the values.
B4X:
BC.Initialize(bmp1.Width,bmp1.Height)

u can also use more break points and let the app continue from one to other to find this problem.
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
check this part if the bitmap have it size, use a breakpoint in debug mode to stop there and watch the values.
B4X:
BC.Initialize(bmp1.Width,bmp1.Height)

u can also use more break points and let the app continue from one to other to find this problem.

Yes, width and height are greater than zero.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
3 points about this line:
B4X:
SQL1.ExecNonQuery2($"INSERT INTO tids (id, name) VALUES (?, '${DateTime.Now}')"$, Array As Object(BC.Buffer))

1. Why not move the time parameter to the array?
2. No need to write Array As Object. Array is enough.
3. The error happens because BitmapCreator.Buffer returns a raw representation of the bitmap. It is not a valid bitmap file data.

ImageToBytes: https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#post-445167

Correct code:
B4X:
SQL1.ExecNonQuery2($"INSERT INTO tids (id, name) VALUES (?, ?)"$, Array(ImageToBytes (Bmp), DateTime.Now))
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
3 points about this line:
B4X:
SQL1.ExecNonQuery2($"INSERT INTO tids (id, name) VALUES (?, '${DateTime.Now}')"$, Array As Object(BC.Buffer))

1. Why not move the time parameter to the array?
2. No need to write Array As Object. Array is enough.
3. The error happens because BitmapCreator.Buffer returns a raw representation of the bitmap. It is not a valid bitmap file data.

ImageToBytes: https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#post-445167

Correct code:
B4X:
SQL1.ExecNonQuery2($"INSERT INTO tids (id, name) VALUES (?, ?)"$, Array(ImageToBytes (Bmp), DateTime.Now))

Solved!
 
Upvote 0
Top