10-04
09

Acdsee中的功能,进行类型,覆盖原有图片,重新定义质量

Acdsee中的功能,进行类型,覆盖原有图片,重新定义质量

public class Test {
    
    //static String filepath = "e:\tmp";

    
    /**
     * 文件的拷贝及其质量处理
     * @param filePath
     * @throws Exception
     */
    public void copyAndQuality(String filePath) throws Exception {
        
        File _file = new File(filePath); // 读入文件
        
        int pos = filePath.lastIndexOf(".");
        
        String ext = filePath.substring(pos+1, filePath.length());//扩展名
        
        Image src = ImageIO.read(_file); // 构造Image对象
        
        int width = src.getWidth(null); // 得到源图宽
        
        int height = src.getHeight(null); // 得到源图长

        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        
        image.getGraphics().drawImage(src, 0, 0, width, height, null);

        ImageWriter writer = null;
        
        ImageTypeSpecifier type = ImageTypeSpecifier
                .createFromRenderedImage(image);

        Iterator iter = ImageIO.getImageWriters(type, ext);
        
        if (iter.hasNext()) {
            writer = (ImageWriter) iter.next();
        }
        if (writer == null) {
            return;
        }

        IIOImage iioImage = new IIOImage(image, null, null);
        
        
        FileUtil fu = new FileUtil();
        
        
        if(fu.isExists(filePath))
            fu.delFile(filePath);
        
        
        
        ImageWriteParam param = writer.getDefaultWriteParam();

        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

        param.setCompressionQuality((float) (5 / 10.0));
        
        ImageOutputStream outputStream = ImageIO
                .createImageOutputStream(new File(filePath));//"c:\\张某人."+ext
        
        writer.setOutput(outputStream);
        
        writer.write(null, iioImage, param);
        
        outputStream.close();
    }

    /**
     * 读取文件夹中的文件
     *
     * @param filepath
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static boolean readFiles(String filepath)
            throws FileNotFoundException, IOException {
        
        try {
            File file = new File(filepath);
            if (!file.isDirectory()) {
            } else if (file.isDirectory()) {
                System.out.println("文件夹");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        System.out.println("path=" + readfile.getPath());
//                        System.out.println("absolutepath="
//                                + readfile.getAbsolutePath());
                        //System.out.println("name=" + readfile.getName());
                      String path =readfile.getPath();
                      try {
                        new Test().copyAndQuality(path);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    } else if (readfile.isDirectory()) {
                        readFiles(filepath + "\\" + filelist[i]);
                    }
                }

            }

        } catch (FileNotFoundException e) {
            System.out.println("readfile() Exception:" + e.getMessage());
        }
        return true;
    }

    public static void main(String[] args) {
//        try {
//            new Test().jpgTset();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        String filepath = "e:/test";
        
        try {
            new Test().readFiles(filepath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: acdsee 图片转换 图片质量
相关日志:
评论: 0 | 引用: 0 | 查看次数: 230
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭