How to find format of an image when its byte[] content alone is available?

Hi,
I'm working on code that gets an image content as input in byte[] format. With no other further details provided, how to identify the format of the image contained in the byte array? Specifically, I would like to know if the image contained in it is a PNG, JPG, BMP, etc.
Image or ImageIO API does not seem to provide a means to do this. Is there a way how this can be achieved?
Thanks in advance.

The ImageIO class does provide a way to do what you want... it just takes a couple of steps. Take a look at the determineInputFormat() method below:
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class ImageFormatTest {
   private static final String IMAGE_PATH = "... PATH TO IMAGE HERE ...";
   public static void main( String[] args ) {
      try {
         final byte[] imageBytes = readImageBytes( IMAGE_PATH );
         final String imageFormat = determineImageFormat( imageBytes );
         System.out.println( "Image format is " + imageFormat );
      } catch (Throwable ex) {
         ex.printStackTrace();
   private static byte[] readImageBytes( String imagePath ) throws IOException {
      final File imageFile = new File( imagePath );
      final byte[] buf = new byte[ (int) imageFile.length() ];
      final FileInputStream fStream = new FileInputStream( imageFile );
      fStream.read( buf );
      fStream.close();
      return buf;
   private static String determineImageFormat( byte[] imageBytes ) throws IOException {
      final ByteArrayInputStream bStream = new ByteArrayInputStream( imageBytes );
      final ImageInputStream imgStream = ImageIO.createImageInputStream( bStream );
      final Iterator<ImageReader> iter = ImageIO.getImageReaders( imgStream );
      final ImageReader imgReader = iter.next();
      return imgReader.getFormatName();
}

Similar Messages

  • How do you properly tag linked images when importing XML content?

    I'm importing content from an Access database into ID CS2 using XML. So far so good with the text and numbers--I'm having difficulty with the graphics, though. There's an image associated with each record and I want InDesign to place the appropriate image in a placeholder frame.
    Right now I have a field in the database which contains the relative path to the image file for each record, but when I import the XML into ID, the link is displayed as text in the graphics frame.
    How do I solve this?
    Thanks.

    Hi,
    Give the path to the image as an attribute href.
    example:
    <image href="../my/path/to/the/image.jpg">  (if the path is relative to the indesign-file)
    or
    <image href="C:/my/path/to/the/image.jpg">
    Greetz,
    Glen

  • How to find a dead iphone for when its offline?

    any suggestions
    hello ive lost my iphone 4. it saysits off;ine and the battery is dead.is there anyway u guys can help me and turn it online

    Hi, this is a user forum, not an Apple help desk. Unfortunately there's no way of tracking your phone in the circumstances you described.

  • How can I improve quality of image when burning a movie to a DVD

    How can I improve quality of image when burning a movie to a DVD?

    What resolution are you using?  If the DVD is to be playable on an ordinary DVD player, only ordinary TV resolution is supported.  You need Blue ray for HD.
    Geoff.

  • How to find the ME29N 'USER EXIT' when our save the release PO after.

    How to find the ME29N 'USER EXIT' when our save the release PO after.
    which user exit will be used?
    thanks.
    alex.

    Hi,
      process :
        go to tranx code :   ME29N ,
           System---->Status , here copy the program name (SAPLMEGUI),
         go to abap editor and specify the program name SAPLMEGUI and find the package "ME".
         tranx code : CMOD ,  Utilites--->Sap Enanchement,
                                         specify the package name : ME
                                          and execute, displays a list of exits ,
                                 and find out our suitable exit based on requirement.
    check these :   EXIT_SAPMM06E_021
    reg
    Siva

  • How to find density of an image

    how to find density of an image
    how to find density of an image
    how to find density of an image
    how to find density of an image
    SrikanthSheshagiri

    @SrikanthSheshagiri
    I've delete your another duplicate case and you consult how to find density of an image use jquery? Could you please check that why you post this kind of question in WPF forum? It seems this is not related to WPF forum scope.
    Best regards,
    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • How can I scroll around an image if its zoomed out?

    Hey guys, quick one for you.
    I have no problem using the hand tool/spacebar to scroll around an image when zoomed into it, but as soon as I can see the whole thing I can't scroll around anymore and its just locked to the center
    I am a After Effects user so I find this a bit annoying that I can't do this, so I figure there MUST be a setting somewhere I could change to enable it.
    Thanks!

    by Buko wrote:
    Sorry but "How can I scroll around an image if its zoomed out?" has got to be one of the stupidest question I've heard on this forum in some time. Brandon really needs to think before posting.
    I've noticed your usual vitriol toward people having simple questions has ramped up about 200% since the advent of Jive, Buko. Maybe you need to disengage, or neck some holy fermented blue agave juice and take a nap?
    I see the question as perfectly cromulent, and I use the suggestion I've given quite often. Apparently, a good reason why anyone would want to do that has escaped you, and it's made you even crankier than usual. So you react by giving the gift of vile condescension instead of just ShuttingTFU and letting someone else handle it. (Next time you blast Ramon for the same thing, take a look at your own behavior). And apparently, it's not enough for you to just accept that the way someone else wants to work is different than your workdflow, or is something that never even occurred to you. Maybe some folks just want an answer to their question, and secretly wish you'd shove your bad attitude up your [asterisk-substituted name for your lower-dorsal body opening]. Of course, some folks are just too polite, or they're new visitors here and won't say that to you because they don't know the legacy players and behaviors here. But I'm not either of those things, and I will. Because if you can dish it out, surely you can take it, right? RIGHT!?!?
    Now...why do I think the question is worth answering? Sometimes I want to be able to have an entire smallish image visible at 100% magnification in order to view it as it will appear in its final destination. AND I want to do some work on it while keeping all my currently-open Palettes visible; maybe I want to move my image out of the way of a big dialogue box I've called up. (I'm only working on one monitor...sorry if that makes your hardened jaw drop down to your chest).
    In Single-Window made mode I'd have to grab and move the whole window, but I can't do that if I have a big dialogue window open. In full screen mode I can scroll around and see all parts of that image at 100% without having to Tab my palettes to their hidden state, or shove my open dialogue nearly off-screen.
    Think past your own nose, Dude! And LightenTFUp!

  • I'm using iphone 5 but its camera is not properly working. all the images i'm clicking are of dimension 600X800 and the size of the image is just 60-70 kb. now please tell me how to increase the dimension of images and its size.

    i'm using iphone 5 but its camera is not properly working. all the images i'm clicking are of dimension 600X800 and the size of the image is just 60-70 kb. now please tell me how to increase the dimension of images and its size.

    ''rojere [[#question-1055991|said]]''
    <blockquote>
    i try to keep my thumb palms off the mouse pad when i am typing but every now and then my screen resizes larger or smaller until i take my left palm off the keyboard base its only when my left palm is resting on the base next to the touch pad. i type pretty fast so it gets annoying and yes it also jumps screens or reverts back to previous message and i have to choose ctrl z to undo and get back to my message i was typing... it also switches tabs on me as well... not sure why my palm being on the left of the keypad on the base should effect anything but it does after a while. and i have to stop what i am doing...and go to the firefox menu and change the screen size back down to 100% sometimes it goes up to 200-300 other times it goes down to 70-80% i find it annoying i i wish there was a way to turn off the screen resize in the setup panel. if its just left to be 100% i am happy with that... i don't need it to be bigger or smaller... there should be a way to LOCK it down... i understand that there are people out there with eye problems and needs to have the screen size increase using the + or - but there should be a lock so it just stays no matter what is happening on my keypad or touch pad or base of the pad... that screen size will not increase or decrease. unless i turn off the lock... perhaps this is something they can implement in the next version or do a quick update of the browser
    </blockquote>
    sorry that is all greek to me thanks for attempting to explain what you were trying to explain sorry i just not tech person to be able to understand or where to begin ... have good day and thanks for again trying

  • HT1212 How do I reset my sons Ipad, when its states that its Disabled and to connect to itunes, and when I do its states that its locked and I need to entre that password, but I cant because its DISABLED!

    How do I reset my sons Ipad, when its states that it's disabled and to conect to itunes, and when I do i get a message thats that says I need to entre a password, but I cant as the Ipad is Disabled

    Ive found the solution,
    TillyLove13 wrote:
    Sorry to hear, I am having the same issue myself, I entered a new passcode last night after i synced the new ios 7 cant rememeber my numbers all i remember is i used 2 and 3 and i got it to where i disabled my ipad. I used the connect to itunes as my restore but its not giving me any options thats helpfull it says restore and update im thinking maybe wipe my ipad and than put the new ios 7 in but nope seens mine was synced yesterday it only restores it back to previous locked position. im trying to get some way around it myself as soon as i find out ill make you aware. good luck. im sorry your probably just as flusterated as me

  • How do you unlock an ipod touch when its been disabled

    how do you unlock an ipod touch when its disabled

    http://support.apple.com/kb/ht1212
    Troubleshooting 101
    Some topics covered
    -Basic troubleshooting steps
    -Hard reset instructions
    -Manual restore instructions
    -Hardware troubleshooting
    -Preventive Maintenance
    -Anti-theft & recovery tips
    -Battery-saving tips
    -Repair options
    -Contacting Apple
    17" 2.2GHz i7 Quad-Core MacBook Pro  8G RAM  750G HD + OCZ Vertex 3 SSD Boot HD 
    Got problems with your Apple iDevice-like iPhone, iPad or iPod touch? Try Troubleshooting 101
     In Memory of Steve Jobs 

  • How to find my lost iphone4 if someone erase all content and settings ?

    how to find my lost iphone4 if someone erase all content and settings ? there is way to find my iphone with serial number ?

    No. However, if your phone was running iOS 7.0 or higher, whoever erased it won't be able to re-activate it without knowinh your Apple ID/Password. Called Activation Lock, & the phone will be useless to whoever has it.

  • Find format of an image

    Hi,
    I'm looking for finding the format of an image file (or stream) : tiff, jpg, gif, png, bmp, ...
    I don't want to use the extention name (.jpg, .tif, ...), but the contain of the file himself.
    how can I do it with JAI (without JDK 1.4) or an other java librairy please ?

    See http://gd.tuwien.ac.at/opsys/linux/java/JAI/1.0.2/i386/?file=jai1_0_2-beta-sample-linux.tar.bz2&type=listing

  • How to stop MG3520 from dropping image when fit to page is checked

    How can I stop MG3520 from cropping image when fit to page is checked?

    Hi Keithlad,
    You will want to make sure that the paper size selected in the printer driver is the same size as the paper that you have loaded in the printer to ensure that the image prints correctly.   When the paper size selected in the printer driver is smaller than the actual page size, the page image will be reduced.  When the paper size selected is larger, the page image will be enlarged. 
    This didn't answer your question or issue? Find more help at Contact Us.
    Did this answer your question? Please click the Accept as Solution button so that others may find the answer as well.

  • How can we split the image when its added dynamically ?

    Hi Experts,
    I have an image where i'm calling a DAL in Entry tab Data section under section level properties.As you know that will execute during Entry process.In that DAL i'm Adding an image based on the user choice during entry.
    When image added that should be accomedate the spaces availale in the form.First half of the section can accomedate in the first page and secound half will go to next page.
    I have used CANSPLITIMAGE rule,but that is not get executed during entry process.
    Can someone help me How to split the image during entry process when its added dynamically ?
    Thanks,
    RAMAN C.

    Aside from what you might think of as the origin placement, Gendata rules are not run during Entry operations. As such, you are not able to do what you describe in versions before 12.2. Starting in 12.2, you still can't execute Gendata rules, however there is a newly supported feature when DAL adds a section that will look for the existence of the CanSplitImage rule and assigns an internal attribute that will allow the section to split during entry. This isn't the same as running the rule, but the net effect should be acceptable. (At some point in the future, perhaps this internal attribute will become something you can just set when adding the section on the form and you won't have to specify the CanSplitImage rule at all - for Batch or Entry. That would be great.) In the meantime, if you are not using 12.2, your only option is to break your section up into smaller section components and add them separately. That should help minimize unused space on the page when something doesn't fit.

  • @FINDSTR - How to find an element containing . (dot) when . (dot) is wildcharacter

    I'm checking a LogFile containing FileName(s) and want to Include/Exclude files according to FileType.
    How to use @FINDSTR  to find an element containing . (dot) when . (dot) is wildcharacter...???

    PowerShell ${"%MainDir%\%FullPath%\%FullFileName%"} = Get-Content ${"%MainDir%\%FullPath%\%ListFileName%"}
    |Select-String '\.mp3|\.mp4|\.m4a|\.flac|\.cue|\.ogg|\.ape|\.wav|\.wv'
    That wont work. Two things wrong
    First thing
    Get-Content expects a filename  like Get-Content c:\music.txt  
    or  Get-Content
    "%MainDir%\%FullPath%\%ListFileName%"  
    Get-Content ${c:\music.txt} will try to get the filename in the file c:\music.txt and not use c:\music.txt
    itself.
    Second Thing
    ${"%MainDir%\%FullPath%\%FullFileName%"} = is wrong, don't use the " inside the { }, the { } act as a
    quotes
    for example ${C:\My Documents\Music.txt} = is ok, that will work, this is a variable
    PowerShell ${"%MainDir%\%FullPath%\%FullFileName%"} = Get-Content ${"%MainDir%\%FullPath%\%ListFileName%"}
    |Select-String '\.mp3|\.mp4|\.m4a|\.flac|\.cue|\.ogg|\.ape|\.wav|\.wv'
    Should be 
    PowerShell ${%MainDir%\%FullPath%\%FullFileName%} = Get-Content "%MainDir%\%FullPath%\%ListFileName%"
    |Select-String '\.mp3|\.mp4|\.m4a|\.flac|\.cue|\.ogg|\.ape|\.wav|\.wv'

Maybe you are looking for

  • Issue with FP_JOB_OPEN /CLOSE

    Hi, I am working on Multiple form outputs.It is like printing multple invoices.My goal is to get all invoices in only single pdf file. I have called FM's FM_JOB_OPEN & CLOSE in loop and endloop. Although i see a single spool number i still see multip

  • Change taxcode in local PO using BBP_DET_TAXCODE_BADI

    Hello Gurus, We are in SRM7.0 extended classic scenario, we want to change the default tax code while creating a local PO and want the new tax code to be displayed in local PO. We have implemented BADI "BBP_DET_TAXCODE_BADI" and changing the tax code

  • What do you do when a download will not launch?

    have a macbook pro, early 2008 and tried to download free trial of scrivener writing software but when i click on it from the applications folder the symbol appears on the dock and bounces. i answer that i want to accept it but it will not launch. on

  • IPhone 4S  Microphone Wont Work

    My microphone quit working.  It will not work for emails, texts or siri....help?

  • Bitrate video Quality

    Hello, I'm broadcasting live video using FMS , what bitrate should I use to broadcast in low or medium or high video quality, I'm using this AS3 code: camera.setMode(250, 180, 15, false); camera.setQuality(350, 50); camera.setKeyFrameInterval(30); mi