Problem with getting resized image's bytes

I've got a problem with getting correct bytes of a newly resized image. The flow is that I retrive an image from the filesystem. Due to the fact that it is large I resize it to a 50x50px thumbnail. I can display this thumbnail in #benchmark1 (see code below). Unfortunately something's wrong with my imageToBytes funtion which returns reasonably small size of image but is totally useless - I can't make an image of it anymore so at #benchmark2 the application either crashes or keeps freezing. I saved this byte array on my disk and tried to preview under Windows how does it look but I got a message "Preview unavailabe". I did some digging in the Internet and I supposed that it's because I don't use any jpg or png encoders to save the file. Actually I think that it's not the case, as the bytes returned from method imageToBytes look weird - I cannot even make a new image of them and display it without any saving in memory.
                              byte[] bytes = FileHandler.readFile (
                                      FileHandler.PHOTOS_PATH, fileName);
                              Image img2 = Image.createImage (bytes, 0, bytes.length);
                              img2 = ImageHandler.getInstance ().resize (img2);
                                                //#benchmark1
                              bytes = ImageUtils.imageToBytes (img2);
                              img2 = Image.createImage (bytes, 0, bytes.length);
                                                //#benchmark2my imageToBytes function is as follows:
     public static byte[] imageToBytes (Image img)
          int[] imgRgbData = new int[img.getWidth () * img.getHeight ()];
          byte[] imageData = null;
          try
               img.getRGB (imgRgbData, 0, img.getWidth (), 0, 0, img.getWidth (),
                       img.getHeight ());
          catch (Exception e)
          ByteArrayOutputStream baos = new ByteArrayOutputStream ();
          DataOutputStream dos = new DataOutputStream (baos);
          try
               for (int i = 0; i < imgRgbData.length; i++)
                    dos.writeInt (imgRgbData);
               imageData = baos.toByteArray ();
               baos.close ();
               dos.close ();
          catch (Exception e)
          return imageData;
I've run totally out of any idea what's wrong, please help!
Edited by: crawlie on Jul 17, 2010 6:21 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Hey Crawlie,
Please note that simply writing int values into ByteArrayOutputStream will not suffice. Please have a look at the following conversion from int pixel value into byte[4]:
public static final byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value
}Good Luck!
Daniel

Similar Messages

  • Problem with displaying BLOB images on JSP page using a servlet

    hi. I have a big problem with displaying BLOB images using JSP. I have a servlet that connects to the oracle database, gets a BLOB image , reads it, and then displays it using a BinaryStream. The problem is , this works only when i directly call that servlet, that is http://localhost:8080/ImageServlet. It doesn't work when i try to use that servlet to display my image on my JSP page (my JSP page displays only a broken-image icon ) I tried several coding approaches with my servlet (used both Blob and BLOB objects), and they work just fine as long as i display images explicitly using only the servlet.
    Here's what i use : ORACLE 10g XE , Eclipse 3.1.2, Tomcat 5.5.16 , JDK 1.5
    here is one of my image servlet's working versions (the essential part of it) :
                   BLOB blob=null;
              rset=st.executeQuery("SELECT * FROM IMAGES WHERE ID=1");
              while (rset.next())
                   blob=((OracleResultSet)rset).getBLOB(2);
              response.reset();
              response.setContentType("image/jpeg");
              response.addHeader("Content-Disposition","filename=42.jpeg");
                    ServletOutputStream ostr=response.getOutputStream();
                   InputStream istr=blob.getBinaryStream(1L);
                    int size=blob.getBufferSize();
              int len=-1;
                    byte[] buff = new byte[size];
                         while ((len=istr.read( buff ))!=-1 ) {
                   ostr.write(buff,0,len);
             response.flushBuffer();
             ostr.close(); and my JSP page code :
    <img src="/ImageServlet" border="0"  > If you could just tell me what i'm doing wrong here , or if you could show me your own solutions to that problem , i would be very greatful ,cos i'm realy stuck here , and i'm rather pressed for time too. Hope someone can help.

    I turns out that it wasn't that big of a problem after all. All i had to do was to take the above code and place it into another JSP page instead of into a servlet like i did before. Then i just used that page as a source for my IMG tag in my first JSP. It works perfectly well. Why this doesn't work for servlets i still don't know, but it's not a problem form me anymore . Ofcourse if someone knows the answer , go ahead and write. I would still appriceatte it.
    here's the magic tag : <img src="ImageJSP.jsp" border="0"  > enjoy : )

  • Just moved my email from entourage to Mail. Having real problems with getting my signatures to work properly. When adding in a small company logo, windows computers only receive signature as an attachment. Am sending email as Rich Text. Any ideas on a fix

    Just moved my email from Entourage to Mail. Having real problems with getting my signatures to work properly despite sending email as Rich Text.
    When adding in a small company logo to the signature, PC's / windows computers only receive signature and logo as attachments.
    I've tried all possible fixes I can find including getting a PC user to format the logo but no joy. Has anyone experienced this and any ideas on a fix gratefully received.

    Send it as html so the signature is an image source URL

  • DW CS3 problem with PNG-8 images in Design View?

    DW CS3 (build 3481) on XP seems to have problems with PNG-8 images, specifically trying to display them in design view.
    I had a very simple image that was able to be exported by Fireworks CS4 as PNG-8 using 4 colors (no transparency) for a tiny file size under 250 bytes.
    DW could not display it at all, or even recognize it, reporting it was 0 x 0 in size. I published the page anyway, and it looked fine in the browser.
    I upped the number of colors to 32, and DW could then display it in design view in the page I was working on.
    I'm still having problems with other PNG-8 images. The attached file "temp.jpg" is a screenshot showing part of the design view in DW.
    The smaller image on the left of the banner is "58_certificate_of_discharge.png" (attached), and the larger image below is used as the background of the DIV containing the form,"compassbk.png" (also attached).
    These images all look fine in the browser, but terrible in DW.
    I'm stumped as to why.
    Thanks in advance.

    According to a different discussion, the images that are stuck as "Queued" are showing a bug in the virus scanning on the forums.
    Now trying the recommended 'camera icon' method...
    Looks like it worked.
    Now compare what it looks like in DW CS3 in the design view:
    It's a 16 color PNG-8 made in Fireworks CS4 with no transparency, size is 113 x 58, ending up at 1.71K.
    If this is a repeatable problem in CS3, is there any hope of it being resolved, or am I stuck with upgrading to CS4 as my only option (assuming it's fixed in CS4)?
    Thanks.

  • Hi, I have a problem with getting my apple Id working for me. It's been 2 months since it happened and Apple failed to act. I can tell my story proerly, but am not sure, you guys can help, so I just copy my message to them today, I am trying to get it acr

    Hi, I have a problem with getting my apple Id working for me. It's been 2 months since it happened and Apple failed to act. I can tell my story proerly, but am not sure, you guys can help, so I just copy my message to them today, I am trying to get it across all the places around to pay their attention. This is a desperate move, so if you are not the right people to help me to get my message accross, may be you can advise where can I go.
    Thank you, and sorry for the language.
    Vitas Dijokas
    I am sorry to say that, but your security makes my life miserable – it’s been 2 months since my Apple ID account got stuck, and since then I cannot update 37 applications (to date), i.e. most of my applications. Half of them I bought. I also paid for iCloud, and it is not working. I paid money and I am stuck with old applications and no iCloud. Your security *****. Your service ***** too. It took your service 1 month to finally understand why this happened to me, and it took me tens of emails to you and 3 hours of telephone conversation to find out the reason for my problem. And the problem is still not fixed. NOT FIXED. You just leave your customer – the one who paid you money and spent so much time with you trying to help you help me – and nothing. You tell me:  “Vitas, Stick your stinky iphone in your *** and enjoy life, we do not care!” *************.
    It is ******* outrageous, and you should know that,  guys. Get into the ******* database and correct the bug. Get someone in the partners-telephone carriers company (it is Orange as carreer and Cellcom as seller of the phone)  authorized to Identify me in personal encounter in one of the branches in Israel (where I live) and make sure it is really me, and get the ******* system accept my password and let me use my phone.
    Otherwise **** off. None of my friends will get my advise to buy an iphone or any of apple products. And I think you should be very attentive to cases like this, guys. Do your work for the money we pay, or disappear. There are many others eager to take your place, and if the problem is not fixed I will eventually go to the others. My patience is lost, and as soon as I can afford another phone I will change it. AND I WILL TRY TO GIVE BAAAAAD PUBLICITY TO APPLE – I am threatening here, so ACT NOW.
    Vitas Dijokas

    Well, it seems waiting is not my strong suit..! I renamed a javascript file called recovery to sessionstore. This file was in the folder sessionstore-backups I had copied from mozilla 3 days ago, when my tabs were still in place. I replaced the sessionstore in mozilla's default folder with the renamed file and then started mozilla. And the tabs reappeared as they were 3 days ago!
    So there goes the tab problem. But again when I started mozilla the window saying "a script has stopped responding" appeared, this time the script being: chrome//browser/contenttabbrowser.xml2542
    If someone knows how to fix this and make firefox launch normally, please reply! Thank you

  • Problems with getting my Ipod Touch to work on my Sony Bravia or any TV

    I am having problems with getting videos or movies to play on my Sony Bravia HDTV or any TV. I have a first generation Ipod Touch and I have the Apple AV Composite Cables and all I get is sound for both TV's that I tried. I read one post where some guy had a faulty Ipod that wouldn't work. Does anyone know how of something hidden or special that needs to be done or how did they fix the problem if they had the same one? Thank, ejcarlson

    Check connection, if you have a case on your Ipod the connection may not be good enough.

  • Problem with getting iPod to work.

    I'm having a problem with getting an iPod to connect to my computers. Also I can't even really get it to run. My brother gave it to me one or two years ago and it had these problems I just didn't think of the idea of going directly to Apple to figure it out. It's a 30 GB black iPod if that helps. When I plugged it in it showed the battery symbol. When I leave it for a while it shows the Apple symbol. Then it comes up with this: http://spirrwell.webatu.com/ipod.jpg If you can help thanks in advance!
    Message was edited by: Spirrwell

    I figured out that it was a hard drive problem so I'll find a fix if not I guess I'm screwed.

  • Problem with getting the little blackberry icon

    Hi the blackberry I'm using is working and up to date. I was setting up my mates blackberry 9320 but having a problem with getting the little blackberry icon to appear in the top right corner of my phone where the GMS is and signal bar and stuff its something to do with blackberry.
    I tried downloading bbm and it say associating bbm with blackberry id and constant loads with nothing happening bbm not working
    I NEED THE TINY BB ICON AT TOP RIGHT???

    Hi neal123
                            That BlackBerry icon will appear near signal bar when we activate  BIS on a 3G device. Did he activated BIS on his account. ? 
    Click " Like " if you want to Thank someone.
    If Problem Resolves mark the post(s) as " Solution ", so that other can make use of it.

  • TS2755 I am having a problem with getting notified when I receive a call or a text.  My phone will not ring or vibrate.

    I am having a problem with getting notified when I receive a call or a text.

    Hi BigBroMAC,
    The first thing I would check is Do Not Disturb mode, as this will cause that behavior:
    iOS 6: Using Do Not Disturb
    http://support.apple.com/kb/HT5463
    If that isn't the issue, this article has slightly different symptoms but the troubleshooting would be the same:
    iPhone: Can't hear through the receiver or speakers
    http://support.apple.com/kb/TS1630
    Before you do step 10, however, I recommend resetting all settings to see if that helps.  To do that, choose Settings > General > Reset > Reset All Settings.
    I hope this helps!
    - Ari

  • I still have problems with getting my website online. I have defined my server. Then I did the test and there was a connection via FTP. I put my files on the external server and there is a connection with the external server. But when I check to see my we

    I still have problems with getting my website online. I have defined my server. Then I did the test and there was a connection via FTP. I put my files on the external server and there is a connection with the external server. But when I check to see my website online (with Firefox, Explorer, Chrome browser) I always get the message 'Forbidden, You don't have permission to access / on this server.' Can somebody help me please? I have to get my website online..Thank you!

    Hello Els,
    it's well known, that in all these cases you describe I'm not a friend of a detailed Troubleshooting (I see Nancy#s smile already).
    To be able to be independent in all this things It is one of the reasons why I prefer an external FTP program. The difficulties with which you have to fight encourage me in this opinion, not least because we always search for experts, we don't charge a "jack of all trades".
    To manage several websites or to upload my files and sometimes for the opposite way, for a necessary download from my server or to use a "a site-wide synch", I'm using FileZilla. It simply looks easier for me to keep track of all operations precisely and generate or reflect easily the desired tree structure.
    Above all, FileZilla has a feature (translation from my German FileZilla) called "compare file list". Here it's possible to use file size or modification time as a criterion. There is also the possibility to "hide identical files", so that only these files which you want to redact remain visible.
    And even if it means you have to install a new program, I am convinced that there is an advantage. Here is the link to get it and where you can read informations about how it works:
    http://filezilla-project.org/ and http://wiki.filezilla-project.org/Tutorial#Using_the_site_manager
    Mac: Mac OS X (Use: Show additional download options)
    http://filezilla-project.org/download.php
    Of course, you also need all the access data to reach your server and for MIME issues, you should contact your web host/provider.
    Good luck!
    Hans-Günter
    P.S.
    Since I use two screens, the whole thing became even more comfortable.

  • Problem in getting the image coordinates  when i zoom in or zoom out

    Hi,
    i have doubts with scaling (Zoom in and Zoom out) using following code but when i zoom in want to get the x and y coordinates but im getting resized coordinates , pls do the needful
    public class TiffTest extends JFrame implements MouseListener
    public TiffTest(String filename)
    RenderedImage source= JAI.create("fileload",filename);
    SubsampleBinaryToGrayDescriptor sb=new SubsampleBinaryToGrayDescriptor();
    RenderingHints rh=new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_DEFAULT);
    RenderedOp op=sb.create(source,new Float(0.5),new Float(0.5),rh);
    JScrollPane scrollPane = new JScrollPane();
    displayjai dj=new displayjai(op);
    scrollPane.setViewportView(dj);
    getContentPane().add(scrollPane,BorderLayout.CENTER);
    int screen_width=(int)this.getToolkit().getScreenSize().getWidth();
    int screen_height=(int)this.getToolkit().getScreenSize().getHeight();
    setVisible(true);
    setSize(screen_width,screen_height);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    dj.addMouseListener(this);
    JAI.create("filestore",dj.ima,"god.tif","TIFF");
    public static void main(String s[])
    new TiffTest("1_org.tif");
    public void mouseClicked(MouseEvent e){System.out.println(e.getX()+","+e.getY());}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    class displayjai extends DisplayJAI
    RenderedOp ima;
    public displayjai(RenderedOp inputImage)
    super(inputImage);
    ima=inputImage;
    setPreferredSize(new Dimension(ima.getWidth(),ima.getHeight()));
    System.out.println(inputImage.getWidth()+","+inputImage.getHeight());
    Thankx
    Kannan

    hi all,
    I am getting the image from the server.but the problem is i am getting the Exception as ArrayOutOfBound exception.
    It was displaying the Image for the first time.and when ever it was going for the second time in to the paint method it was displaying the IOException in reading the stream from the connection.
    can anybody give me the solution in rectifying that exception.
    thanks in advance
    lakshman

  • Problem in getting the image through URL

    hi all,
    I facing the problem,Inwhich i am unable find the solution...I am using the following code to display the image
    public void doGet(HttpServletRequest request,HttpServletResponse response)
                        throws IOException, ServletException {
                   int data=0;
                   response.setContentType("image/png");
                   ServletOutputStream out = response.getOutputStream();
                   String file = request.getContextPath()+imageNames[0];
                   //String file = "C:/Program Files/Apache Group/Tomcat 4.1/webapps/ImageComm/WEB-INF/images"+imageNames[index];
                   BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
                   System.out.println("the size of the inputStream is..."+in.available());
                   System.out.println("the context path is..."+request.getContextPath());          
                   while ((data = in.read()) != -1) {
                   out.write(data);
    with the above i am not getting any error at the compile time but it was giving exception like FileNotFoundException.
    but the same thing(URL address) when i am copying on the browser it was displayig the image.
    (what might be the reason)
    one more thing when i commented on the url address and try to use the actual realpath address it was displaying the image with out any problem
    can anybody give me the solution like where to keep my images by which i can able to get the images through url address
    thanks in advance
    lakshman

    hi all,
    I am getting the image from the server.but the problem is i am getting the Exception as ArrayOutOfBound exception.
    It was displaying the Image for the first time.and when ever it was going for the second time in to the paint method it was displaying the IOException in reading the stream from the connection.
    can anybody give me the solution in rectifying that exception.
    thanks in advance
    lakshman

  • Problems with the background image size in a photobook

    Hi there, I really need help here. When I make a photobook I want to display my images large, taking the whole page. To do that I put the image as a background and it's OK. My problem is that when I do this, iPhoto always crops it. I have to use the move tool to move the image up and down, left or right to chose a best position. I don't want to do this, I want my image to be shown perfectly from end to end. I have to resize the image in many ways, even used the book's native resolution but it still doesn't work. If I choose "fit photo to frame size" I end up with two stripes at the top and the bottom of the page.
    Please help me, what size should I resize my image to, so that iPhoto displays it as it is?
    Thank you

    Hi
    The fact you are using a Mac is why both myself and JCellini assumed you where using a pre IE7 version.
    But the code you are using to test in IE will cause a problem with any 'virtual machine' such as crossover, because it cannot reference anything outside its own assigned section of the drive, and referencing the png in the way you are would be for it an invalid reference, it also would not work in any browser when you uploaded the site.
    Change - background-image:url(file:///Macintosh  HD/Users/myname/Documents/websites/test/Test/images/test_back.png);
    To - background-image:url(images/test_back.png);
    But if the region is not editable I would suggest you temporarily make it so, externalize your css to a css file, insert a link to the external css file in your head content, then lock the region once again. This way you will always be able to edit your css without worrying about 'locked regions'.
    PZ
    www.pziecina.com

  • Problems with screen saver image quality

    Just got a MPB 15 inch. Very happy with it but have a small problem with the screen saver. I use my i Photo folders as the source for screen saver images. Some photos are shown in very good quality but others are very poor - as if low res. The original images are all of the same high quality. Seems to occur whichever video setting is used under power saving. A minor issue I now but any ideas?

    Hi, Mark. Apparently some of the photos being used are low-resolution thumbnails, not full-resolution images.
    What version of iPhoto do you have, and which OS version are you running? On my MBP running 10.5.6 and iPhoto 7.1.5, "All folders" is not an option in the Screensaver pane of System Prefs, so I don't know what you're actually choosing. But if I select "Pictures" (which refers to the Pictures folder in my Home folder), then the screensaver uses every .jpg file that it finds in any of the several iPhoto Libraries inside my Pictures folder. The thumbnails created by iPhoto are .jpgs, so they get used by the screensaver along with full-size high-resolution images.
    The solution is not to select a folder that contains any thumbnails.

  • Problem with a product image/pdf production in Indesign CS5

    Hi,
    this problem probably has an obvious solution, but I can't figure it out due to my limited experience in using Indesign. I'm trying to export my poster lay-out as pdf, but one of the several product images does not show up in the exporte pdf correctly. Instead of a product image, I only get this white image that is similar to the shape of the product image, but the actual product image is not visible. The product image is a .psd -file and it shows correctly in InDesign document, but not in the exported .pdf file. Could somebody help me to tackle this problem?
    BR,
    Masa

    Hi,
    - It shows in the preview mode
    - I'm using only one layer in the layout with all the images on it
    - The image is not non-printing in the Attributes panel
    - Links have no errors and theya re up to date
    When I create the PDF via FILE > PRINT, then the problem does not occur, but I have another problem with white margins on both top and bottom of the .pdf-file. ¨
    I still would like to create the pdf via Export.
    Thank you for your quick  reply, what's next?
    BR,
    Masa

Maybe you are looking for

  • CS6 crashes every time I try to watch an image sequence. Why?

    I am trying to make an image sequence but after it is made and i put it in the time line when i go to watch it Premiere crashes. Here is the crash report. Process:         Adobe Premiere Pro CS6 [6126] Path:            /Applications/Adobe Premiere Pr

  • Broadband speed is low.

    Hi, I did a speedtest on the 1st and it showed I was getting 16mb DL speed however in the past couple of days I have only been recieving 3-4mb sometimes even slower. I checked my status through ADSL settings and was wondering if the noise on the line

  • Failed itunes installation 10.1.4.62 windows 7

    i installed and reinstalled new itunes 10.1.4.62 win7 but the program dont open. Never had problems before with other itunes version. now i cant open itunes and i cant do sincro and back uo for my iphone. I insert image of error during installation a

  • Regarding uploadind file , pics , etc ,etc from desktop to DMS server.

    Hi friends , I am a fresher and i dont know anything abt DMS .. I have given a task of uploading a file from desktop to DMS server ....Our requirement is not to use FTP server ......plzz guide me guys from very first step .... it wud b of gr8 help 2

  • New PDF Ports

    Hi , I am working with Adobe 8.1.4 in Vista. I want to know how add a new port. Currently I have two ports [ Desktop/*.pdf and Documents/*.pdf]. I want to add my personal folder as a output  something like C:\Balaji\*.pdf. When I click the Add Port a