IOException Jpeg Lib

Dataverde

Member
Licensed User
Longtime User
God evening,

i need some help getting this to work.
The sub should load a smaller version of the choosen picture if it is wider or higher than the ImageView it is ment for. Very similar to Agrahams Jpg Demo.
:sign0148:
Thank you very much for taking a look,

Lennart


Global Variables and Programm start:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim zoomActive As Boolean
   Dim selectionActive As Boolean
   
   Dim startSelectionX As Int
   Dim startSelectionY As Int
   
   Dim selectionBorderWith As Float: selectionBorderWith = 3.0
   
   
   Dim selectBtn As Button
   Dim zoomBtn As Button
   Dim touchPanel As Panel
   Dim picturePart As ImageView
   Dim selectionOverlay As ImageView
   
   'Dim picturePartCanvas As Canvas
   Dim selectionOverlayCanvas As Canvas
   Dim content As ContentChooser
   content.Initialize("content")

   
   Dim device As Phone
   Dim jpg As Jpeg
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("oneSizeFitsAll")
   Dim menuIcon As BitmapDrawable
   menuIcon = device.GetResourceDrawable(17301567) 'gallery
   Activity.AddMenuItem2("Open picture","openPictures",menuIcon.Bitmap)
   
   zoomActive = False
   initializeViews
   jpg.Initialize("jepg")
End Sub

The sub that should load the picture:
B4X:
Sub content_Result(Success As Boolean, Dir As String, FileName As String)
   If Success Then
      Dim picturePartCanvas As Canvas
      Dim input As InputStream
      picturePartCanvas.Initialize(picturePart)
      input = File.OpenInput(Dir,FileName)
      jpg.LoadJpegSizeOnly(input)
      Log(jpg.JpegHeight&" > "&picturePart.Height&" ODER "&jpg.JpegWidth&" > "&picturePart.Width&"?")
      If jpg.JpegHeight > picturePart.Height OR jpg.JpegWidth > picturePart.Width Then
   Log("true: picture is BIG")
         Dim highDiff As Int
         Dim widthDiff As Int
         highDiff = Floor(jpg.JpegHeight/picturePart.Height)+1
         widthDiff = Floor(jpg.JpegWidth/picturePart.Width)+1
         Dim diffToUse As Int: diffToUse = Max(highDiff,widthDiff)
         'bildVerkleinert laden mit faktor diffToUse
         Dim tmpRect As Rect
         tmpRect.Initialize(0,0,picturePart.Height,picturepart.Width)
   Log("highDiff = "&highDiff&" widthDiff = "&widthDiff&" Max = "&diffToUse)
         input = File.OpenInput(Dir,FileName)
         Dim tmpBitmap As Bitmap
         tmpBitmap.Initialize3(jpg.LoadJpegSmaller(input,diffToUse))
         picturePartCanvas.DrawBitmap(tmpBitmap,Null,tmpRect)
         picturePart.Invalidate
         DoEvents
      Else
   Log("false: picture is small")
      End If
      input.Close
   Else
      ToastMessageShow("Error loading picture",False)
   End If
End Sub

Log:
B4X:
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
running waiting messages (1)
1920 > 724 ODER 2560 > 480?
true: picture is BIG
highDiff = 3 widthDiff = 6 Max = 6
main_content_result (B4A line: 118)
tmpBitmap.Initialize3(jpg.LoadJpegSmaller(input,diffToUse))
java.io.IOException
   at java.io.InputStream.reset(InputStream.java:221)
   at anywheresoftware.b4a.agraham.jpeg.Jpeg.LoadJpegSmaller(Jpeg.java:145)
   at dont.showMy.face.main._content_result(main.java:432)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:89)
   at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:829)
   at anywheresoftware.b4a.BA$2.run(BA.java:313)
   at anywheresoftware.b4a.BA.setActivityPaused(BA.java:263)
   at dont.showMy.face.main$ResumeMessage.run(main.java:190)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3835)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
   at dalvik.system.NativeStart.main(Native Method)
java.io.IOException
 

Dataverde

Member
Licensed User
Longtime User
I tried to close it first. Same error.

Also see this in the Demo App:
B4X:
   in = File.OpenInput(File.DirAssets, ImageName)
   Jpg.LoadJpegSizeOnly(in)
   in = File.OpenInput(File.DirAssets, ImageName)   
   bmp = Jpg.LoadJpegSmaller(in, Jpg.JpegHeight/ImageView1.Height)
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
All the Jpeg LoadXxxxx methods close the input stream so that is not a problem.

I'm afraid that I have no idea what your problem could be as it just should not occur! :confused: Have you tried a different image?
B4X:
  public Bitmap LoadJpegSmaller(InputStream instream, int denominator) throws Exception
  {
    instream.mark(Integer.MAX_VALUE); // mark the start of the stream so we can rewind to it 
    j.decode(instream, this, 0, 0, 0, 0, true); // this the same as LoadJpegSizeOnly to get the size
    instream.reset(); //this is Java line 145 where the exception occurs! I've no idea why
    .... // read the stream again to decode and resize "on the fly" to save memory
 
Upvote 0

Dataverde

Member
Licensed User
Longtime User
Thank you for the help!
Im going to test this the next time i have time for this little side project(image manipulation is something i want to learn more about).
 
Upvote 0
Top