• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

resize an image stored in byte array

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,

i have a bean 'PhotoDetails' in which photo is a member of byte[]
i am retieving this object from database .
PhotoDetails objPhoto = dao.getPhotoDetails(Id);

before displaying this image i want to adjust the image size. can any one tell how can get the size of the image and resize it?

Thaks & Regards ,
Anuja Karthikeyan
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you need to know what format (GIF, JPEG, TIFF etc.) your photo is in. If you don't know, and only have an array of bytes, your task is impossible (well, maybe you can "sniff" the format by looking at the bytes, but that's dodgy).

If you do know the format, you can read the bytes into a Java BufferedImage using ImageIO operations. When you have a BufferedImage, you can do things like AffineTransform on it, to change size, shape etc. Then you can write out to a different array of bytes, using ImageIO again.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
Well, you need to know what format (GIF, JPEG, TIFF etc.) your photo is in. If you don't know, and only have an array of bytes, your task is impossible (well, maybe you can "sniff" the format by looking at the bytes, but that's dodgy).

If you do know the format, you can read the bytes into a Java BufferedImage using ImageIO operations. When you have a BufferedImage, you can do things like AffineTransform on it, to change size, shape etc. Then you can write out to a different array of bytes, using ImageIO again.



ImageIO can do that sniffing for you!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew that image files often contain initial bytes that identify the type of content. But I didn't know that such initial bytes would also be found in a byte array. I guess, though, that the byte array is often just the contents of some files, so it makes sense.

In that case, there's probably nothing wrong with "sniffing" the content type. It would still be safer to hard-code the content type if you know it will never change. I'd perhaps then suggest an assertion that checks that the hard-coded content type matches the "sniffed" one, where available.
 
We can walk to school together. And we can both read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic