jpeg - How can I read the DPI for a JPG file on Android? -


is there java library can read dpi jpg file (like pngj png)? code on android can't use java.awt.image.*

and if there's way android bitmap class, awesome.

according wikipedia, how jpeg metadata works :

first 2 bytes 0xffd8 (soi specified here). then, if there metadata, 0xffe0 (2 bytes)

if there metadata, there length segment(2 bytes) identifier segment(5 bytes : 0x4a46494600) , version segment (2 bytes) , density:

density units 1 byte: 0 none specified, 1 ppi, 2 ppc .

then x density (2 bytes) , y density (2 bytes).

so , in short, have skip 2+2+2+5+2=13 bytes , read 1 byte type of density units, , if it's not 0 , read 4 bytes density values (x , y) .

hope can help.


i curious check out, i've made nice snippet worked sample jpg file. hope work file, haven't read article well:

final inputstream inputstream=getresources().openrawresource(r.raw.tt); try   {   inputstream.skip(13);   final int densitytype=inputstream.read();   switch(densitytype)     {     case 0:       log.d("debug","no density specified");       break;     case 1:       log.d("debug","density in ppi");       break;     case 2:       log.d("debug","density in ppc");       break;     }   if(densitytype!=0)     {     final byte[] densityvalue=new byte[2];     inputstream.read(densityvalue);     final int xdensity=(densityvalue[0]<<8)+densityvalue[1];     inputstream.read(densityvalue);     final int ydensity=(densityvalue[0]<<8)+densityvalue[1];     log.d("debug","xdensity:"+xdensity+" ydensity:"+ydensity);     }   } catch(final ioexception e)   {} try   {   inputstream.close();   } catch(final ioexception e)   {} 

edit: think "length" bytes shouldn't ignored represent "length of segment excluding app0 marker" , if it's small, can't read density part , might read things don't have density.

i hope point though, , it's not hard fix code.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -