java 獲取圖片屬性、破損圖片處理、寫入日誌、連線資料庫

Alex_longjiu11發表於2014-06-30

類CheckPhotos程式碼

package readPhoto;



import java.awt.Container;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


import javax.imageio.ImageIO;


import org.apache.log4j.Logger;


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGDecodeParam;
import com.sun.image.codec.jpeg.JPEGImageDecoder;


public class CheckPhotos {
private static Logger logger = Logger.getLogger(CheckPhotos.class);
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("D:\\jkk_img");
check(file);
}
//獲取圖片的屬性值
public static void check(File file) throws FileNotFoundException, IOException{

File[] files =file.listFiles();
for(File f:files){
try{
if(f.isDirectory()){
check(f);
}else{
String filename=f.getName();
if(filename.endsWith(".jpg")){
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new FileInputStream(f));
BufferedImage image = decoder.decodeAsBufferedImage();
JPEGDecodeParam param = decoder.getJPEGDecodeParam();
int xDensity = param.getXDensity();
int yDensity = param.getYDensity();
BufferedImage sourceImg =ImageIO.read(new FileInputStream(f)); 
double mm=Double.valueOf(String.format("%.1f",f.length()/1024.0));
int width = sourceImg.getWidth();
int height= sourceImg.getHeight();
if(width!=358 || height!=441 || mm < 15 ||xDensity!=96 ||yDensity!=96 ){
logger.info(xDensity);
logger.info("檔案"+filename+"過小");
System.out.println(xDensity);
System.out.println(yDensity);
System.out.println(mm); //大小 
System.out.println(sourceImg.getWidth());  //畫素
System.out.println(sourceImg.getHeight()); //畫素
System.out.println(filename);
}
}
}
}catch(Exception e){
e.printStackTrace();
System.out.println(f.getName());
go1(f);
}
}
}
//處理被損壞的圖片
public static void go1(File file){
BufferedImage bis = null; 
  try{ 
      bis = ImageIO.read(file); 
      bis.getWidth(); 
      bis.getHeight(); 
  }catch(Exception e){ 
   try 
   { 
   //這兩句程式碼,是處理cmyk型別的圖片,需要ImageMagick的支援
    ThumbnailConvert tc = new ThumbnailConvert(); 
    tc.setCMYK_COMMAND(file.getPath()); 
    Image image =null; 
    image = Toolkit.getDefaultToolkit().getImage(file.getPath()); 
    MediaTracker mediaTracker = new MediaTracker(new Container()); 
    mediaTracker.addImage(image, 0); 
    mediaTracker.waitForID(0); 
    System.out.println(image.getWidth(null));
    System.out.println(image.getHeight(null));
    double mm=Double.valueOf(String.format("%.1f",file.length()/1024.0));
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new FileInputStream(file));
JPEGDecodeParam param = decoder.getJPEGDecodeParam();
int xDensity = param.getXDensity();
System.out.println(xDensity);
int yDensity = param.getYDensity();
System.out.println("chuio"+yDensity+"?");
    System.out.println(mm);
   }catch (Exception e1){ 
    e1.printStackTrace(); 
   } 
  } 
}
//破損圖片處理
public static void go(File file) {
BufferedImage bis = null;
try {
bis = ImageIO.read(file);
bis.getWidth();
bis.getHeight();
} catch (Exception e) {
try {
ThumbnailConvert tc = new ThumbnailConvert();
tc.setCMYK_COMMAND(file.getPath());
Image image = null;
image = Toolkit.getDefaultToolkit().getImage(file.getPath());
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
image.getWidth(null);
image.getHeight(null);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}

}


類ThumbnailConvert 

package readPhoto;


import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class ThumbnailConvert {
private String CMYK_COMMAND = "mogrify -colorspace RGB -quality 80 file1";//轉換cmyk格式   
public void setCMYK_COMMAND(String file1) {   
exeCommand(CMYK_COMMAND.replace("file1", file1));   
}   
 
public boolean exeCommand(String cmd){   
InputStreamReader ir = null;   
LineNumberReader input = null;   
try   
{   
//linux下java執行指令:Runtime.getRuntime().exec (String str);   
Process process = Runtime.getRuntime().exec (cmd);   
ir=new InputStreamReader(process.getInputStream());   
input = new LineNumberReader (ir);   
while ((input.readLine ()) != null){   
}   
ir.close();   
input.close();   
}   
catch (java.io.IOException e){   
System.err.println ("IOException " + e.getMessage());   
return false;   
}   
return true;   
}  
}

相關文章