Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating Images from Primefaces (JSF) fileupload InputStream

Posted on 2011-03-24
4
Medium Priority
?
3,467 Views
Last Modified: 2012-05-11
Hello,

When I try to convert an InputStream from the primefaces fileupload component I just get black images.  I want to convert png and gif to jpg but this happened going from a jpg to a jpg, any help is greatly appreciated!

    public void handleFileUpload(FileUploadEvent event) { 
    		UploadedFile tfile = event.getFile();
    		String str = tfile.getFileName();
    		//String ext = str.substring(str.lastIndexOf('.'), str.length());
    		String fileName = utilities.getLoggedInUser().getId() + "_" + new Date().getTime();
    		String filePath = uploadPath + fileName;
    		
    		try {
    			imageService.createDreamImages(event.getFile().getInputstream(), filePath, "jpg", 800, 200);
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    
    			e.printStackTrace();
    		}
    
    public class ImageResizeImpl implements ImageResizeService{
    
    		public void createDreamImages(InputStream in, String location, String fileType, int imageSize, int imageThumbSize) throws Exception{
    			try {
    				BufferedImage image = ImageIO.read(in);
    				ImageIO.write(resizeTrick(image, imageSize, imageSize), fileType, new File(location + "." + fileType));
    				ImageIO.write(resizeTrick(image, imageThumbSize, imageThumbSize), fileType, new File(location + "_t." + fileType));
    			} 
    			catch (IOException e) {
    				throw new Exception();
    			}
    		}
    }

Open in new window

0
Comment
Question by:cgray1223
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35212896
as a test try it without the resize, if that works ok we know the problem is with the resize code
0
 

Author Comment

by:cgray1223
ID: 35213111
yeah it works otherwise, here is the whole implementation


@Named("imageService")
	public class ImageResizeImpl implements ImageResizeService{

		public void createDreamImages(InputStream in, String location, String fileType, int imageSize, int imageThumbSize) throws Exception{
			try {
				BufferedImage image = ImageIO.read(in);
				ImageIO.write(resizeTrick(image, imageSize, imageSize), fileType, new File(location + "." + fileType));
				ImageIO.write(resizeTrick(image, imageThumbSize, imageThumbSize), fileType, new File(location + "_t." + fileType));
			} 
			catch (IOException e) {
				throw new Exception();
			}
		}

		private static BufferedImage resize(BufferedImage image, int width, int height) {
			int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
			BufferedImage resizedImage = new BufferedImage(width, height, type);
			Graphics2D g = resizedImage.createGraphics();
			g.setComposite(AlphaComposite.Src);
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.drawImage(image, 0, 0, width, height, null);
			g.dispose();
			return resizedImage;
		}

		private static BufferedImage resizeTrick(BufferedImage image, int width, int height) {
			image = createCompatibleImage(image);
			image = resize(image, 100, 100); 
			image = blurImage(image);
			return resize(image, width, height);
		}

		public static BufferedImage blurImage(BufferedImage image) {
			float ninth = 1.0f/9.0f;
			float[] blurKernel = {
					ninth, ninth, ninth,
					ninth, ninth, ninth,
					ninth, ninth, ninth
			};

			Map<RenderingHints.Key, Object> map = new HashMap<RenderingHints.Key, Object>();
			map.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			map.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
			map.put(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
			RenderingHints hints = new RenderingHints(map);
			BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, blurKernel), ConvolveOp.EDGE_NO_OP, hints);
			return op.filter(image, null);
		}

		private static BufferedImage createCompatibleImage(BufferedImage image) {
			GraphicsConfiguration gc = BufferedImageGraphicsConfig.getConfig(image);
			int w = image.getWidth();
			int h = image.getHeight();
			BufferedImage result = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
			Graphics2D g2 = result.createGraphics();
			g2.drawRenderedImage(image, null);
			g2.dispose();
			return result;
		}

	}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35213533
>                   image = createCompatibleImage(image);

comment out that line and tell me if that works
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 35220563
jpg doesn't support transparency so you'll need to convert any pixels with transparency to a solid colour

>                   BufferedImage result = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);

any reason you're not using the transparency setting from source image
as in this example
http://helpdesk.objects.com.au/java/how-to-convert-an-image-to-a-bufferedimage
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question