Dynamically Resizing an Image?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gotejjeken
    New Member
    • Sep 2007
    • 27

    #1

    Dynamically Resizing an Image?

    I have an image stored in a database using a byte[]. Now, when I read it from the database I'd like to resize it to a certain size so I can avoid having to smash it using HTML.

    Does anyone know of a way to do this?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by Gotejjeken
    I have an image stored in a database using a byte[]. Now, when I read it from the database I'd like to resize it to a certain size so I can avoid having to smash it using HTML.

    Does anyone know of a way to do this?
    Try this page of the graphics 2D tutorial. I see the example at the bottom of the page can resize images.

    http://java.sun.com/docs/books/tutor...drawimage.html

    Is it possible to resize first and save the resized version? It would save on having to repeatedly resize it later, if it is only to one size it needs to be converted.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Demo:
      [CODE=Java]
      import java.awt.*;
      import java.awt.geom.* ;
      import java.awt.image. *;
      import java.io.*;
      import java.net.*;
      import javax.imageio.* ;
      import javax.swing.*;

      public class ImageExample implements Runnable {
      private BufferedImage[] images;

      public ImageExample(Bu fferedImage... images) {
      this.images = images;
      }

      public void run() {
      JFrame f = new JFrame();
      Container cp = f.getContentPan e();
      cp.setLayout(ne w GridLayout(1,0) );
      for(BufferedIma ge image : images) {
      cp.add(new JLabel(new ImageIcon(image )));
      }
      f.pack();
      f.setDefaultClo seOperation(Win dowConstants.EX IT_ON_CLOSE);
      f.setLocationRe lativeTo(null);
      f.setVisible(tr ue);
      }

      public static BufferedImage resizeAnImage(B ufferedImage in, double scale) {
      AffineTransform tx = AffineTransform .getScaleInstan ce(scale, scale);
      AffineTransform Op op = new AffineTransform Op(tx, AffineTransform Op.TYPE_BICUBIC );
      return op.filter(in, null);
      }

      public static void main(String[] args) throws IOException {
      URL url = new URL("http://blogs.sun.com/jag/resource/JagHeadshot-small.jpg");
      BufferedImage image1 = ImageIO.read(ur l);
      BufferedImage image2 = resizeAnImage(i mage1, 1.5);
      BufferedImage image3 = resizeAnImage(i mage1, 0.6);
      EventQueue.invo keLater(new ImageExample(im age1, image2, image3));
      }
      }[/CODE]

      Comment

      • Gotejjeken
        New Member
        • Sep 2007
        • 27

        #4
        Using your demo I have this:

        Code:
            private static byte[] resizeAnImage(UploadFile in, String type, int scale) 
            {
            	//--Create a new BufferedImage from our byte[]
            	BufferedImage bm = new BufferedImage(scale,scale,BufferedImage.TYPE_INT_RGB);
            	try
        		{
            		bm = ImageIO.read(in.getInpuStream());
        		}
            	catch(Exception e)
        		{
            		System.err.println("Exception: " + e);
        		}
            	
            	//--Scale it
                AffineTransform tx = AffineTransform.getScaleInstance(scale, scale);
                AffineTransformOp op = new AffineTransformOp(tx, 3);
                
                //--Dies here
                op.filter(bm, null);
                
                //--Convert it back to a byte[]
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                try
        		{
                    ImageIO.write(bm, type, output);
        		}
                catch(Exception e)
        		{
                	System.err.println("Exception: " + e);
        		}
                
                byte[] bytesOut = output.toByteArray();
                
                return bytesOut;
            }
        Bascially I'm taking a file from an HTML upload box and attempting to convert it to a BufferedImage, resize it, and then convert it to a byte[] for database storage.

        With the code above however, the image will upload, however it will not be resized.

        Any suggestions?

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          First, I think you meant to write:
          [CODE=Java]bm=op.filter(bm , null);[/CODE]
          not
          [CODE=Java]op.filter(bm, null);[/CODE]
          a resizing filtering clearly can't do that in place.

          As to running out of memory, that error says it all. First, I would try to get this working for smaller images, then work my way up to larger ones. Start small.

          Comment

          • Gotejjeken
            New Member
            • Sep 2007
            • 27

            #6
            Doh! Thanks. It looks as if it is working now, I originally was trying to use 100 as a scale value before I noticed your example was MUCH smaller than that. I take it the function resizes in inches not pixels?

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by Gotejjeken
              Doh! Thanks. It looks as if it is working now, I originally was trying to use 100 as a scale value before I noticed your example was MUCH smaller than that. I take it the function resizes in inches not pixels?
              In my code, the scale parameter is a scaling factor, not a fixed size in some units. So if scale = 0.5, it will create a new image whose width and height are half the original. If scale is 100, it will be a hundred times higher and wider -- 10,000 times bigger, in area! If you want to create an image of a certain size, work backwards from that to figure out what the scaling factor should be.

              Comment

              • Gotejjeken
                New Member
                • Sep 2007
                • 27

                #8
                I see. Thanks for all the help.

                Comment

                Working...