473,473 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamically Resizing an Image?

27 New Member
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?
Dec 12 '07 #1
7 4884
BigDaddyLH
1,216 Recognized Expert Top Contributor
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.
Dec 12 '07 #2
BigDaddyLH
1,216 Recognized Expert Top Contributor
Demo:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import java.awt.image.*;
  4. import java.io.*;
  5. import java.net.*;
  6. import javax.imageio.*;
  7. import javax.swing.*;
  8.  
  9. public class ImageExample implements  Runnable {
  10.     private BufferedImage[] images;
  11.  
  12.     public ImageExample(BufferedImage... images) {
  13.         this.images = images;
  14.     }
  15.  
  16.     public void run() {
  17.         JFrame f = new JFrame();
  18.         Container cp = f.getContentPane();
  19.         cp.setLayout(new GridLayout(1,0));
  20.         for(BufferedImage image : images) {
  21.             cp.add(new JLabel(new ImageIcon(image)));
  22.         }
  23.         f.pack();
  24.         f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  25.         f.setLocationRelativeTo(null);
  26.         f.setVisible(true);
  27.     }
  28.  
  29.     public static BufferedImage resizeAnImage(BufferedImage in, double scale) {
  30.         AffineTransform tx = AffineTransform.getScaleInstance(scale, scale);
  31.         AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
  32.         return op.filter(in, null);
  33.     }
  34.  
  35.     public static void main(String[] args) throws IOException {
  36.         URL url = new URL("http://blogs.sun.com/jag/resource/JagHeadshot-small.jpg");
  37.         BufferedImage image1 = ImageIO.read(url);
  38.         BufferedImage image2 = resizeAnImage(image1, 1.5);
  39.         BufferedImage image3 = resizeAnImage(image1, 0.6);
  40.         EventQueue.invokeLater(new ImageExample(image1, image2, image3));
  41.     }
  42. }
Dec 12 '07 #3
Gotejjeken
27 New Member
Using your demo I have this:

Expand|Select|Wrap|Line Numbers
  1.     private static byte[] resizeAnImage(UploadFile in, String type, int scale) 
  2.     {
  3.         //--Create a new BufferedImage from our byte[]
  4.         BufferedImage bm = new BufferedImage(scale,scale,BufferedImage.TYPE_INT_RGB);
  5.         try
  6.         {
  7.             bm = ImageIO.read(in.getInpuStream());
  8.         }
  9.         catch(Exception e)
  10.         {
  11.             System.err.println("Exception: " + e);
  12.         }
  13.  
  14.         //--Scale it
  15.         AffineTransform tx = AffineTransform.getScaleInstance(scale, scale);
  16.         AffineTransformOp op = new AffineTransformOp(tx, 3);
  17.  
  18.         //--Dies here
  19.         op.filter(bm, null);
  20.  
  21.         //--Convert it back to a byte[]
  22.         ByteArrayOutputStream output = new ByteArrayOutputStream();
  23.         try
  24.         {
  25.             ImageIO.write(bm, type, output);
  26.         }
  27.         catch(Exception e)
  28.         {
  29.             System.err.println("Exception: " + e);
  30.         }
  31.  
  32.         byte[] bytesOut = output.toByteArray();
  33.  
  34.         return bytesOut;
  35.     }
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?
Dec 13 '07 #4
BigDaddyLH
1,216 Recognized Expert Top Contributor
First, I think you meant to write:
Expand|Select|Wrap|Line Numbers
  1. bm=op.filter(bm, null);
not
Expand|Select|Wrap|Line Numbers
  1. op.filter(bm, null);
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.
Dec 13 '07 #5
Gotejjeken
27 New Member
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?
Dec 13 '07 #6
BigDaddyLH
1,216 Recognized Expert Top Contributor
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.
Dec 13 '07 #7
Gotejjeken
27 New Member
I see. Thanks for all the help.
Dec 13 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alex Hopson | last post by:
I'm using the code below to loop through some images and resize each image twice, once to create a thumbnail and once to create a small image. The page stops loading around the 38th image out of...
13
by: Jon Yeager | last post by:
I need to display a bunch of pictures that are all of various dimensions into a fixed dimension space, like MSN Messenger does with its user photos in the chat windows. Forcing image dimensions...
9
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with...
5
by: Jim | last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick) is a processor intensive exercise. I'm setting up a site where users will be uploading up to 10 images along with the details...
2
by: clintonG | last post by:
I'd like to learn how to convert a homepage to an image dynamically. Any page for that matter with results embedded in another page as can be observed by this example . Comments? -- <%= Clinton...
1
by: Ron Vecchi | last post by:
I am using asp.net to upload an image and then perform resizing on it and saving the different sizes to file. The resized images were coming up and being displayed in the bowser fine but the image...
10
by: David W. Simmonds | last post by:
I have a DataList control that has an Image control in the ItemTemplate. I would like to resize the image that goes into that control. I have a series of jpg files that are full size, full...
6
by: tomasio | last post by:
Dear NG, years have passed and I am still more designer than programmer. I build a new version of my website which has a few nasty bugs, especially on my startpage: Resizing text brakes the...
4
by: =?Utf-8?B?Tm9ibGUgQmVsbA==?= | last post by:
Hello, I want to know how I can do the following in ASP.Net 2.0: I want to be able to mouse-over a small image and then have it dynamically zoom into a larger image. When the larger image is...
10
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.