Thumbnails操作图片发红的问题解决

这个问题其实是由于这条语句导致的错误    ImageIO.read(new File("watermark.png")), 0.5f),所以我们用bufferedimage构造就不会出现这个问题,下面

ImageWrapper imageWrapper = ImageReadHelper.read(inStream);这句代码引用的是阿里巴巴下的simpleimage里的方法,



	public static void test1() {
		try {
			File in = new File("c:\\img\\in.jpg"); // 原图片
			FileInputStream inStream = new FileInputStream(in);
			ImageWrapper imageWrapper = ImageReadHelper.read(inStream);
			Thumbnails.of(imageWrapper.getAsBufferedImage()).size(160, 160)
					.toFile("c:\\img\\out.jpg");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SimpleImageException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


还有一种解决图片发红的问题

 /** 
     * 生成图片带红的处理方法 
     * @param image 
     * @param width 
     * @param height 
     * @return 
     */  
    public static BufferedImage toBufferedImage(Image image,int width,int height) {  
  
        if (image instanceof BufferedImage) {  
            return (BufferedImage)image;  
        }  
  
        // This code ensures that all the pixels in the image are loaded  
        image = new ImageIcon(image).getImage();  
  
        // Determine if the image has transparent pixels; for this method's  
        // implementation, see e661 Determining If an Image Has Transparent Pixels  
        //boolean hasAlpha = hasAlpha(image);  
  
        // Create a buffered image with a format that's compatible with the screen  
        BufferedImage bimage = null;  
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
  
        try {  
            // Determine the type of transparency of the new buffered image  
            int transparency = Transparency.OPAQUE;  
           /* if (hasAlpha) { 
                transparency = Transparency.BITMASK; 
            }*/  
  
            // Create the buffered image  
            GraphicsDevice gs = ge.getDefaultScreenDevice();  
            GraphicsConfiguration gc = gs.getDefaultConfiguration();  
            bimage = gc.createCompatibleImage(width, height, transparency);  
        } catch (HeadlessException e) {  
            // The system does not have a screen  
        }  
  
        if (bimage == null) {  
            // Create a buffered image using the default color model  
            int type = BufferedImage.TYPE_INT_RGB;  
            //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang  
            /*if (hasAlpha) { 
                type = BufferedImage.TYPE_INT_ARGB; 
            }*/  
  
            bimage = new BufferedImage(width, height, type);  
        }  
  
        // Copy image to buffered image  
        Graphics g = bimage.createGraphics();  
        // Paint the image onto the buffered image  
        g.drawImage(image, 0, 0,width,height, null);绘制缩小的图  
        g.dispose();  
        return bimage;  
    }  
      


你可能感兴趣的:(JAVASE学习笔记)