B4A Library Jpeg library

Cameras on devices are getting higher in resolution but displaying those images is not possible owing to memory limitations on the device. This library can decode arbitrary areas of large images without having to read the entire image into memory.

Unfortunately this can be rather slow. One reason is because Jpegs are optimally compressed with variable length coding and so it is not possible to locate and "jump" to the required point in the file to begin decoding because this cannot be found without decompressing the entire file up to that point.

However the main reason this process can be slow is because versions of Android before 2.2 interpret the bytecodes of their programs at runtime. Versions 2.2 of Android and later have a Just In Time compiler that compiles the bytecodes to native code and, according to reports that I have read, provide a speed increase of 4 or 5 times. The CPUs used by typical Android devices are increasing in their processing speed which again should give a processing speed advantage to the most recent devices. So locating the spaceman's head in the demo takes about 7 seconds on my 600Mhz Android 2.1 phone and comes down to around one second on a 1Ghz Android 2.2 or later device.

EDIT :- Version 1.1 now posted. See post #22 for details.

Note that if you like to test it in rapid debug mode then you need to add #DebuggerForceStandardAssets: true to your project.
 

Attachments

  • Jpeg1.1.zip
    344 KB · Views: 1,831
Last edited by a moderator:

agraham

Expert
Licensed User
Longtime User
Version 1.1 now posted has some changes related to the pixel array handling.

LoadJpegSmaller now always returns an ARGB_8888 bitmap. LoadJpegArea always did.

SetPixelsFromBmp now sets BmpWidth, BmpHeight and the new BmpConfig properties.

There is a longstanding bug in Android where pixel data is returned in Int ABGR format values rather than ARGB. PixelsABGRtoARGB is added to adjust the values in the pixel array if required.
 

nacnud

Member
Licensed User
Longtime User
Pixel Array

Hi,

I know I'm being really stupid here but how do I take this array

' if you need to you can get the pixels as an array of Ints
Dim pixels() As Int
pixels = Jpg.BmpPixels
' draw line
Dim i As Int
For i = 0 To 100
pixels(i)= Colors.Yellow
Next

And put it back into an image that I can see on the screen?

Canvas?
Activity.SetBackgroundImage?
Image View?

Can anyone shine some light on this old fool?

Thanks,
Dunk:sign0104:
 

mistermentality

Active Member
Licensed User
Longtime User
I've found that some jpegs give the following error and I was wondering what may be the cause. Could it be because these images contain exif data (they are primarily jpegs taken with a Kodak digital camera although not all pics taken with the same camera give an error)?

The error is:

Jpg.LoadJpegSizeOnly(in) java.lang.Exception: Jpeg error: Frame format error [Lf!=count]

The reason I wonder if it is the exif jpeg data is because it says format error and as I do not understand the technicalities behind compression of jpegs I have no idea what the above error means.

Thank you for any light you can shed on the reason :)

Dave
 

agraham

Expert
Licensed User
Longtime User
I doubt that the problem is the presence of Exif. If it copes with some images with Exif data it should cope with all as it just ignores the data once it sees an Application-specific marker.

Other than the possibility that there is actually a bug in the camera software I am afraid that I have no idea what the problem is.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
As written above you should reference the library:
SS-2011-12-11_09.13.08.png
 

Inman

Well-Known Member
Licensed User
Longtime User
I have been trying to get the color of a pixel from a jpg using this library. I used Jpg.GetBmpPixel got the value -16711423. How can I check if this pixel is in black color?
 

Inman

Well-Known Member
Licensed User
Longtime User
Ok, I managed to figure it out myself

B4X:
Dim col, r, g, b, a As Int, Jpg As Jpeg, in As InputStream

in=File.OpenInput(dir,filename)
Jpg.LoadJpegArea(in,0,0,240,180)
col=Jpg.GetBmpPixel(1,1)
r = Bit.And(Bit.ShiftRight(col,1), 0xff)
g = Bit.And(Bit.ShiftRight(col,8), 0xff)
b = Bit.And(Bit.ShiftRight(col,16), 0xff)
a = Bit.And(Bit.ShiftRight(col,24), 0xff)
 
Top