• 大小: 8KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-19
  • 语言: Java
  • 标签:

资源简介

java图片压缩处理(可以压缩为任意大小)

资源截图

代码片段和文件信息

import java.io.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.awt.*; 
import com.sun.image.codec.jpeg.*; 
import java.awt.image.BufferedImage; 
/** 

* @author Jiangpin 
*/ 
public class Img_Middle { 
    public void img_change(String urlString name) 
    { 
            Tosmallerpic(urlnew File(url+name)“_middle“name188165(float)0.7); 
            Tosmallerpic(urlnew File(url+name)“_small“name4545(float)0.7); 
            Tosmallerpic(urlnew File(url+name)“_smaller“name116100(float)0.7); 
    } 

   private static void img_change11(String urlString name) 
    { 
            File  f=new File(url+name);
            float per =(300*1024)/f.length();
            if(f.length()>300*1024){
                per =(float)(300*1024)/f.length();
                per =(float)(per+(1-per)/5);
            }else{
                
                per=(float)1.0;
            }
            System.out.println(“图片大小:“+f.length()/1024+“K“ );
            System.out.println(“压宿比例:“+per );
            //Tosmallerpic(urlnew File(url+name)“_middle“name1440900(float)0.7); 
            Tosmallerpic(urlnew File(url+name)“_small“name10241024(float)0.7); 
            Tosmallerpic(urlnew File(url+name)“_smaller“name1024500(float)0.7); 
            Tosmallerpic(urlnew File(url+name)“_middle“name1024640(float)per); 
    } 
/** 

* @param f 图片所在的文件夹路径 
* @param filelist 图片路径 
* @param ext 扩展名 
* @param n 图片名 
* @param w 目标宽 
* @param h 目标高 
* @param per 百分比 
*/ 
      private static void  Tosmallerpic(String fFile filelistString extString nint wint hfloat per){ 
            Image src; 
            try { 
                src = javax.imageio.ImageIO.read(filelist); //构造Image对象 

               String img_midname=f+n.substring(0n.indexOf(“.“))+ext+n.substring(n.indexOf(“.“)); 
               int old_w=src.getWidth(null); //得到源图宽 
               int old_h=src.getHeight(null); 
               int new_w=0; 
               int new_h=0; //得到源图长 

               double w2=(old_w*1.00)/(w*1.00); 
               double h2=(old_h*1.00)/(h*1.00); 

               //图片跟据长宽留白,成一个正方形图。 
               BufferedImage oldpic; 
               /*
               if(old_w>old_h) 
               { 
                   oldpic=new BufferedImage(old_wold_wBufferedImage.TYPE_INT_RGB); 
               }else{
                  if(old_w                     oldpic=new BufferedImage(old_hold_hBufferedImage.TYPE_INT_RGB); 
                  }else{ 
                    oldpic=new BufferedImage(old_wold_hBufferedImage.TYPE_INT_RGB); 
                 }     
               }*/
                oldpic=new BufferedImage(old_wold_hBufferedImage.TYPE_INT_RGB);  
                Graphics2D g = oldpic.createGraphics(); 
                g.setColor(Color.white); 
                
                if(old_w>old_h) 
                { 
              

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      30634  2011-09-24 23:25  java图片压缩处理(可以压缩为任意大小,压缩后高清,不变形(留白),可以改后缀名,可以修改压缩分辨率) - - ITeye技术网站.htm

     文件       6141  2011-09-24 23:58  Img_Middle.java

----------- ---------  ---------- -----  ----

                36775                    2


评论

共有 条评论