2018-06-08 14:51:54   Visit  130

Unsupported Image Type

以下方法弃用了,改用twelvemonkeys

<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-jpeg</artifactId>
    <version>3.3.2</version>
</dependency>
public static BufferedImage toBufferedImage(File imgFile, int maxWidth, int maxHeight){
            if(imgFile.exists()){
                try {
                    // ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
                    String types = Arrays.toString(ImageIO.getReaderFormatNames());
                    String suffix = null;
                    // 获取图片后缀
                    if(imgFile.getName().indexOf(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\".\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") > -1) {
                        suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\".\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") + 1);
                    }// 类型和图片后缀全部小写,然后判断后缀是否合法
                    if(suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0){
                        logger.error(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"Sorry, the image suffix is illegal. the standard image suffix is {}.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" + types);
                        return null;
                    }
                    Image img = ImageIO.read(imgFile);
                    // 根据原图与要求的缩略图比例,找到最合适的缩略图比例
                    int width = img.getWidth(null);
                    int height = img.getHeight(null);
                    int[] wh=calcWH(width,height,maxWidth,maxHeight);
                    int w=wh[0];
                    int h=wh[1];
                    if(w==width && h==height){
                        logger.debug(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"宽度高度没有压缩,返回源文件!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
                        return ImageIO.read(imgFile);
                    }
               
                    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                    Graphics g = bi.getGraphics();
                    g.drawImage(img, 0, 0, w, h, null);
                    g.dispose();
                    return bi;
                } catch (IOException e) {
                    logger.error(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"generate thumbnail image failed.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",e);
                    try {
                        Image img ;

                        //Find a suitable ImageReader
                        java.util.Iterator readers = ImageIO.getImageReadersByFormatName(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"JPEG\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
                        javax.imageio.ImageReader reader = null;
                        while(readers.hasNext()) {
                            reader = (javax.imageio.ImageReader)readers.next();
                            if(reader.canReadRaster()) {
                                break;
                            }
                        }
                        //Stream the image file (the original CMYK image)
                        javax.imageio.stream.ImageInputStream input =   ImageIO.createImageInputStream(imgFile); 
                        reader.setInput(input); 

                        //Read the image raster
                        java.awt.image.Raster raster = reader.readRaster(0, null); 
                        raster.getWidth();
                        //Create a new RGB image
                        int width = raster.getWidth();
                        int height = raster.getHeight();
                        int[] wh=calcWH(width,height,maxWidth,maxHeight);
                        int w=wh[0];
                        int h=wh[1];
                        BufferedImage bi;
                        bi = createJPEG4(raster);
                        
                        
                        //bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); 
                        //Fill the new image with the old raster
                        //bi.getRaster().setRect(w/width, h/height,raster); 
                        
                        //g.drawImage(raster, 0, 0, w, h, null);
                        img = bi.getScaledInstance(w, h, Image.SCALE_SMOOTH);
                        
                        BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                        Graphics g = bi2.getGraphics();
                        g.drawImage(img, 0, 0, w, h, null);
                        g.dispose();
                        return bi2;
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        //com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader r = new com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader();
                    }
                }
            }else{
                logger.warn(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"the image is not exist.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
            }
            return null;
        }
    
        private static BufferedImage createJPEG4(Raster raster) {
            int w = raster.getWidth();
            int h = raster.getHeight();
            byte[] rgb = new byte[w * h * 3];
    
            // 彩色空间转换
            float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
            float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
            float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
            float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
    
            for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
                float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
    
                double val = y + 1.402 * (cr - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
                        : (byte) (val + 0.5);
    
                val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
                        : (byte) (val + 0.5);
    
                val = y + 1.772 * (cb - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
                        : (byte) (val + 0.5);
            }
    
            raster = Raster.createInterleavedRaster(new DataBufferByte(rgb,
                    rgb.length), w, h, w * 3, 3, new int[] { 0, 1, 2 }, null);
    
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
            ColorModel cm = new ComponentColorModel(cs, false, true,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
            return new BufferedImage(cm, (WritableRaster) raster, true, null);
        }

参考https://www.aliyun.com/jiaocheng/822407.html

©2017 Leechg.com