zoukankan      html  css  js  c++  java
  • no jpeg in java.library.path;java.lang.NoClassDefFoundError: Could not initialize class sun.awt.image.codec.JPEGImageEncoderImpl

    no jpeg in java.library.path;java.lang.NoClassDefFoundError: Could not initialize class sun.awt.image.codec.JPEGImageEncoderImpl

    因为要压缩图片,所以用到了JPEGImageEncoder类,但这个类在1.7后就被抛弃了,导致现在调用总是报错,虽然我在本地windows eclipse上可以成功调用,但到了centos上就出错了。

    从网上的建议看,就是取而代之用ImageIO类就能解决这个问题。

    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    
    public class ImageUtil {
    	
    	// 压缩图片到制定的宽度,同时长度压缩到相同比例
    	private static final int COMPRESS_WIDTH = 150;
    	
    	// 压缩质量
    	private static final float RATIO = 0.99f;
        /**  
         * 按宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传  
         * @param oldFile  要进行压缩的文件全路径  
         * @param newFile  新文件  
         * @param width  宽度  
         * @param height 高度  
         * @param quality 质量  
         * @return 返回压缩后的文件的全路径  
         */    
        public static boolean compressImage(
        		String oldPath, String newPath) {    
        	File oldFile = new File(oldPath);
        	File newFile = new File(newPath);
        	
            if (!oldFile.exists()) {    
                return false;
            }
            
            String newImage = null;    
            try {    
                /** 对服务器上的临时文件进行处理 */    
                Image srcFile = ImageIO.read(oldFile);    
                int w = srcFile.getWidth(null);    
            //  System.out.println(w);    
                int h = srcFile.getHeight(null);    
            //  System.out.println(h);    
        
                /** 宽,高设定 */ 
                // 压缩后的高
                int height = (int)(1.0*COMPRESS_WIDTH/w*h);
                BufferedImage tag = new BufferedImage(
                		COMPRESS_WIDTH, height
                		,BufferedImage.TYPE_INT_RGB);
                
                tag.getGraphics().drawImage(srcFile, 0, 0, COMPRESS_WIDTH, height, null);    
                //String filePrex = oldFile.substring(0, oldFile.indexOf('.'));    
                /** 压缩后的文件名 */    
                //newImage = filePrex + smallIcon+ oldFile.substring(filePrex.length());    
        
                /** 压缩之后临时存放位置 */    
                
                JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);    
                /** 压缩质量 */    
                jep.setQuality(RATIO, true);    
                
    
    //            FileOutputStream out = new FileOutputStream(newFile);    
    //            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
    //            encoder.encode(tag, jep);    
                
                String formatName = newPath.substring(newPath.lastIndexOf(".") + 1);
                //FileOutputStream out = new FileOutputStream(dstName);
                //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                //encoder.encode(dstImage);
                ImageIO.write(tag, /*"GIF"*/ formatName /* format desired */ , new File(newPath) /* target */ );
    //            out.close();    
            } catch (FileNotFoundException e) {    
                e.printStackTrace();    
            } catch (IOException e) {    
                e.printStackTrace();    
            }    
            return true;    
        }    
    }
    
    
  • 相关阅读:
    【转+补充】在OpenCV for Android 2.4.5中使用SURF(nonfree module)
    Delphi StarOffice Framework Beta 1.0 发布
    Angular ngIf相关问题
    angularjs文档下载
    公众号微信支付开发
    公众号第三方平台开发 教程六 代公众号使用JS SDK说明
    公众号第三方平台开发 教程五 代公众号处理消息和事件
    公众号第三方平台开发 教程四 代公众号发起网页授权说明
    公众号第三方平台开发 教程三 微信公众号授权第三方平台
    公众号第三方平台开发 教程二 component_verify_ticket和accessToken的获取
  • 原文地址:https://www.cnblogs.com/sevenun/p/7441858.html
Copyright © 2011-2022 走看看