1

I need to be able to modify the contents of a jpeg or png files and am able to successfully break the image down into bytes and vice versa. The only problem is that i do not know how many bytes make up a jpeg file header or a png file header. The information on most websites is pretty vague and/or way too informative for a beginner like me.

Id really appreciate if someone can provide a simple answer telling me how many bytes i need to skip to get past the header and how to identify if the image is a jpeg image or a png image as well as any other important information that i may not have mentioned.

Ive added the code below which im using to extract the bytes from an image and to convert the image into bytes.

Note: This code works on android OS

Code used to convert image to bytes:

public  byte[] imgtobytes(File f)
{
 FileInputStream fis=null;

 try
 {
  fis = new FileInputStream(f);
 }

 catch(Exception e)  {}

  Bitmap bm = BitmapFactory.decodeStream(fis);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bm.compress(Bitmap.CompressFormat.JPEG, 100 , baos);
  byte[] b = baos.toByteArray();
  return b;
}

Code used to convert bytes to image and display it on an imageview:

public void bytestoimg(byte[] bytearray, ImageView imgv)
{
    Bitmap bmp = BitmapFactory.decodeByteArray(bytearray, 0, bytearray.length);

    imgv.setImageBitmap(Bitmap.createScaledBitmap(bmp, imgv.getWidth(),
        imgv.getHeight(), false));
}
Maaz_Khan
  • 23
  • 2
  • It will not work to just skip a fixed number of bytes. Instead, find a library that reads the image format. – Henry Aug 27 '19 at 16:43
  • I did find one which reads the image format but i still have to edit the contents and just knowing the image format is not enough. – Maaz_Khan Aug 29 '19 at 10:35
  • This is the link for the code @Henry https://jaimonmathew.wordpress.com/2011/01/29/simpleimageinfo/ – Maaz_Khan Aug 29 '19 at 10:35
  • Really sorry if i am asking newbie questions but i am not very experienced – Maaz_Khan Aug 29 '19 at 10:37

1 Answers1

0

I've done some similar work in python a while ago playing with encryption. I believe that what you are looking for can be found in this wikipedia article under "Examples" section.

enter image description here

Also you can check out this answer

mdzeko
  • 932
  • 10
  • 19
  • I have checked this link before and it was useful but in the jpeg section, after the jfif and exif part, it says there is more metadata. Does this mean i need to skip more bytes to get to the actual image contents? – Maaz_Khan Aug 27 '19 at 13:59
  • Sorry for the inactivity, if you have not yet solved this it might be usefull to dig deeper into JPEG wikipedia article. There is a section "Syntax and structure" that might give additional insight. https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure – mdzeko Aug 28 '19 at 05:52