手机相机图像对比工具开发

驱动需要研究市场上目前流行的各种品牌手机的拍照效果,从而提升公司手机相机功能。收集了大量的不同手机的照片照片进行对比,手机对比速度很慢而且容易出错,顺手开发一个工具解决手工录入问题。最终会把不同相机的图片组装成如下图片。


提供灰度柱状图,厂商,曝光参数,光圈,ISO,白平衡,焦距等的对比,以用于比较相机的成象质量。

程序用到了metadata-extractor-2.9.1.jar和xmpcore-5.1.2.jar两个库

代码如下:


import java.io.File;   
import java.util.ArrayList;   
import java.util.List;   
import java.util.Queue;   
  
/**  
 * @author tiwson 2010-06-02  
 *   
 */  
public class FileSearcher {   
  
    /**  
     * 递归查找文件  
     * @param baseDirName  查找的文件夹路径  
     * @param targetFileName  需要查找的文件名  
     * @param fileList  查找到的文件集合  
     */  
    public static void findFiles(String baseDirName, String targetFileName, List fileList) {   
        /**  
         * 算法简述:  
         * 从某个给定的需查找的文件夹出发,搜索该文件夹的所有子文件夹及文件,  
         * 若为文件,则进行匹配,匹配成功则加入结果集,若为子文件夹,则进队列。  
         * 队列不空,重复上述操作,队列为空,程序结束,返回结果。  
         */  
        String tempName = null;   
        //判断目录是否存在   
        File baseDir = new File(baseDirName);   
        if (!baseDir.exists() || !baseDir.isDirectory()){   
            System.out.println("文件查找失败:" + baseDirName + "不是一个目录!");   
        } else {   
            String[] filelist = baseDir.list();   
            for (int i = 0; i < filelist.length; i++) {   
                File readfile = new File(baseDirName + "\\" + filelist[i]);   
                //System.out.println(readfile.getName());   
                if(!readfile.isDirectory()) {   
                    tempName =  readfile.getName();    
                    if (FileSearcher.wildcardMatch(targetFileName, tempName) || FileSearcher.wildcardMatch("*.JPG", tempName)) {   
                        //匹配成功,将文件名添加到结果集   
                        fileList.add(readfile.getAbsoluteFile());    
                    }   
                } else if(readfile.isDirectory()){   
                    findFiles(baseDirName + "\\" + filelist[i],targetFileName,fileList);   
                }   
            }   
        }   
    }   
       
    /**  
     * 通配符匹配  
     * @param pattern    通配符模式  
     * @param str    待匹配的字符串  
     * @return    匹配成功则返回true,否则返回false  
     */  
    private static boolean wildcardMatch(String pattern, String str) {   
        int patternLength = pattern.length();   
        int strLength = str.length();   
        int strIndex = 0;   
        char ch;   
        for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {   
            ch = pattern.charAt(patternIndex);   
            if (ch == '*') {   
                //通配符星号*表示可以匹配任意多个字符   
                while (strIndex < strLength) {   
                    if (wildcardMatch(pattern.substring(patternIndex + 1),   
                            str.substring(strIndex))) {   
                        return true;   
                    }   
                    strIndex++;   
                }   
            } else if (ch == '?') {   
                //通配符问号?表示匹配任意一个字符   
                strIndex++;   
                if (strIndex > strLength) {   
                    //表示str中已经没有字符匹配?了。   
                    return false;   
                }   
            } else {   
                if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {   
                    return false;   
                }   
                strIndex++;   
            }   
        }   
        return (strIndex == strLength);   
    }   
/* 
    public static void main(String[] paramert) {   
        //    在此目录中找文件   
        String baseDIR = "D:/Pictures - 副本/iPhone 6s";    
        //    找扩展名为txt的文件   
        String fileName = "*.jpg";    
        List resultList = new ArrayList();   
        FileSearcher.findFiles(baseDIR, fileName, resultList);    
        if (resultList.size() == 0) {   
            System.out.println("No File Fount.");   
        } else {   
            for (int i = 0; i < resultList.size(); i++) {   
                System.out.println(resultList.get(i));//显示查找结果。    
            }   
        }   
    }   
*/
  
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class HistogramAnalysisAlg {
	private BufferedImage srcImage;
	private BufferedImage histogramImage;
	private int size = 280;
	
	public HistogramAnalysisAlg(BufferedImage srcImage){
		histogramImage = new BufferedImage(size,size, BufferedImage.TYPE_4BYTE_ABGR);
		this.srcImage = srcImage;
	}
	
	public BufferedImage getHistogram() {
        int[] inPixels = new int[srcImage.getWidth()*srcImage.getHeight()];
        int[] intensity = new int[256];
        for(int i=0; i<intensity.length; i++) {
        	intensity[i] = 0;
        }
        getRGB( srcImage, 0, 0, srcImage.getWidth(), srcImage.getHeight(), inPixels );
        int index = 0;
        for(int row=0; row<srcImage.getHeight(); row++) {
        	int ta = 0, tr = 0, tg = 0, tb = 0;
        	for(int col=0; col<srcImage.getWidth(); col++) {
        		index = row * srcImage.getWidth() + col;
        		ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                int gray = (int)(0.299 * (double)tr + 0.587 * (double)tg + 0.114 * (double)tb);
                intensity[gray]++;
        	}
        }
        
        // draw XY Axis lines
        Graphics2D g2d = histogramImage.createGraphics();
        //g2d.setPaint(Color.BLACK);
        g2d.setPaint(Color.WHITE);
        g2d.fillRect(0, 0, size, size);
        g2d.setPaint(Color.WHITE);
        g2d.drawLine(5, 250, 265, 250);
        g2d.drawLine(5, 250, 5, 5);
        
        // scale to 200
        //g2d.setPaint(Color.GREEN);
        g2d.setPaint(Color.BLACK);
        int max = findMaxValue(intensity);
        float rate = 200.0f/((float)max);
        int offset = 2;
        for(int i=0; i<intensity.length; i++) {
        	int frequency = (int)(intensity[i] * rate);
        	g2d.drawLine(5 + offset + i, 250, 5 + offset + i, 250-frequency);
        }
        
        // X Axis Gray intensity
        //g2d.setPaint(Color.RED);
        g2d.setPaint(Color.BLACK);
        g2d.drawString("Gray Intensity", 100, 270);
		return histogramImage;
	}
	
	private int findMaxValue(int[] intensity) {
		int max = -1;
		for(int i=0; i<intensity.length; i++) {
			if(max < intensity[i]) {
				max = intensity[i];
			}
		}
		return max;
	}

	/**
	 * A convenience method for getting ARGB pixels from an image. This tries to avoid the performance
	 * penalty of BufferedImage.getRGB unmanaging the image.
	 */
	public int[] getRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
		int type = image.getType();
		if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
			return (int [])image.getRaster().getDataElements( x, y, width, height, pixels );
		return image.getRGB( x, y, width, height, pixels, 0, width );
    }

	/**
	 * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance
	 * penalty of BufferedImage.setRGB unmanaging the image.
	 */
	public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
		int type = image.getType();
		if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
			image.getRaster().setDataElements( x, y, width, height, pixels );
		else
			image.setRGB( x, y, width, height, pixels, 0, width );
    }

}


import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifDirectoryBase;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifInteropDirectory;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.exif.ExifThumbnailDirectory;
import com.drew.metadata.jpeg.JpegDirectory;


public class PictureTool {


	public static void main(String[] args) {
		List[] resultList = new List[5];
		int k = 0;
		if (args.length == 0) {
			File directory = new File(System.getProperty("user.dir"));//设定为当前文件夹
			File[] childFiles=directory.listFiles();
			//找出所有子目录 
			System.out.println("directory="+directory.getPath());
			for(int i=0; childFiles!=null && i<childFiles.length;i++)  
			{ 
				if(childFiles[i].isDirectory() && childFiles[i].getPath().indexOf("newx") == -1)
				{
					System.out.println("childFiles1[i]="+i+childFiles[i].getPath());
					String fileName = "*.jpg";
					resultList[k] = new ArrayList();
					FileSearcher.findFiles(childFiles[i].getPath(), fileName, resultList[k]);
					if (resultList[k].size() == 0) {
						System.out.println("No File Fount.");
					} else {
						for (int j = 0; j < resultList[k].size(); j++) {
							System.out.println(resultList[k].get(j));// 显示查找结果。
						}
					}
					k++;
				} 
			}
		} else {

			for (int i = 0; i < args.length; i++) {
				System.out.println(args[i]);
				// baseDirs[i] = args[i];
				String fileName = "*.jpg";
				resultList[i] = new ArrayList();
				FileSearcher.findFiles(args[i], fileName, resultList[i]);
				if (resultList[i].size() == 0) {
					System.out.println("No File Fount.");
				} else {
					for (int j = 0; j < resultList[i].size(); j++) {
						System.out.println(resultList[i].get(j));// 显示查找结果。
					}
				}
			}
		}
		if (resultList[0] != null) {
			for (int i = 0; i < resultList[0].size(); i++) {
				CreatePics(
						resultList[0]!=null?((String) resultList[0].get(i).toString()).replaceAll("\\\\", "/"):null, 
						resultList[1]!=null?((String) resultList[1].get(i).toString()).replaceAll("\\\\", "/"):null,
						resultList[2]!=null?((String) resultList[2].get(i).toString()).replaceAll("\\\\", "/"):null, 
						resultList[3]!=null?((String) resultList[3].get(i).toString()).replaceAll("\\\\", "/"):null, 
						resultList[4]!=null?((String) resultList[4].get(i).toString()).replaceAll("\\\\", "/"):null);
			}
		}
        
	}

	static String s_info = ""; 
	static String fontName = "宋体";
	static int fontStyle = Font.BOLD; 
	static int fontSize = 40;
	static Color fontColor = Color.GREEN;
	static int fontX = 10;
	static int fontY = 10+fontSize;
	static float fontAlpha = 1.0f;
	static Graphics2D graphics = null;
	static BufferedImage bufferedImage = null;
	static int linenum = 5;
	public static void CreatePic(String InImg){
		File file = new File(InImg);
		try {
			Image InImage = ImageIO.read(file);
			int width = InImage.getWidth(null);
			int height = InImage.getHeight(null);
			// 将目标图片加载到内存。
			int w = (int)(width);
			int h = (int)(height);
			boolean isBack = (w > 3000);
			if (isBack){
				w =  (int)(w*0.5);
				h = (int)(h*0.5);
			}
			bufferedImage = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
			graphics = bufferedImage.createGraphics();
			AffineTransform origXform = graphics.getTransform();
		    AffineTransform newXform = (AffineTransform)(origXform.clone());
		    // center of rotation is center of the panel
		    if (isBack){
		        newXform.setToScale(0.5f, 0.5f);
		    }else{
		    	newXform.setToScale(1.0f, 1.0f);
		    }
		    graphics.setTransform(newXform); 
			graphics.drawImage(InImage, 0, 0, null);
			// Reset to Original
			graphics.setTransform(origXform);
			if (isBack){
			    graphics.setFont(new Font(fontName, fontStyle, fontSize));
			}else{
				graphics.setFont(new Font(fontName, fontStyle, fontSize));//30));
			}
			graphics.setColor(fontColor);
			// 设置水印图片的透明度。
			graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
					fontAlpha));
	
			HistogramAnalysisAlg his = new HistogramAnalysisAlg(bufferedImage);
			AffineTransform at = new AffineTransform();
	        at.scale(1.0f, 0.5f);
	        BufferedImageOp bi = new AffineTransformOp(at, null);
			graphics.drawImage(his.getHistogram(), bi, 5, 5);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
        Metadata metadata;
        try {
        	String s = file.getName();
        	
            if (s != null){
            	s_info = "名字:"+s;
            	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                linenum++;
                /*
                graphics.setFont(new Font(fontName, fontStyle, 80));
                graphics.drawString(s_info, 600, 100);
                graphics.setFont(new Font(fontName, fontStyle, fontSize));
                */
            }
            
           
			metadata = ImageMetadataReader.readMetadata(file);
			for (Directory directory : metadata.getDirectories()) {
				
				s = directory.getString(ExifIFD0Directory.TAG_MAKE);
                if (s != null){
                	s_info = "厂商:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifIFD0Directory.TAG_SOFTWARE);
                if (s != null){
                	s_info = "软件:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                
                s = directory.getString(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH);
                if (s != null){
                	s_info = "宽度:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT);
                if (s != null){
                	s_info = "高度:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                
                s = directory.getString(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
                if (s != null){
                	s_info = "日期时间:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_TIME);
                if (s != null){
                	/*
                	String[] ss= s.split("/");
                	float s1=Float.valueOf(ss[0]);
                	float s2=Float.valueOf(ss[1]);
                	int s3=Math.round(s2/s1);
                	System.out.println("1/"+s3);
                	*/
                	s_info = "曝光时间:"+ directory.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_TIME);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_BIAS);
                if (s != null){
                	s_info = "曝光补偿:"+directory.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_BIAS);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_MODE);
                if (s != null){
                	s_info = "曝光模式:"+directory.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_MODE);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_FNUMBER);
                if (s != null){
                	s_info = "光圈:"+ directory.getDescription(ExifSubIFDDirectory.TAG_FNUMBER);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getDescription(ExifSubIFDDirectory.TAG_MAX_APERTURE);//directory.getString(ExifSubIFDDirectory.TAG_MAX_APERTURE);
                if (s != null){
                	s_info = "最大光圈:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT);
                if (s != null){
                	s_info = "ISO:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_FLASH);
                if (s != null){
                	s_info = "闪光灯:"+directory.getDescription(ExifSubIFDDirectory.TAG_FLASH);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_FOCAL_LENGTH);
                if (s != null){
                	s_info = "焦距:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_METERING_MODE);
                if (s != null){
                	s_info = "测光模式:"+directory.getDescription(ExifSubIFDDirectory.TAG_METERING_MODE);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_WHITE_BALANCE_MODE);
                if (s != null){
                	s_info = "白平衡:"+directory.getDescription(ExifSubIFDDirectory.TAG_WHITE_BALANCE_MODE);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                
                
			}
		} catch (ImageProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        graphics.dispose();
        linenum = 5;
		try {
			FileOutputStream outImage = new FileOutputStream(file.getParentFile().getParentFile()+"/new"+file.getName());
			ImageIO.write(bufferedImage, "jpg", outImage);
			outImage.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public static void CreatePics(String InImg1, String InImg2, String InImg3,
			String InImg4, String InImg5) {
		File file1 = null, file2 = null, file3=null, file4 = null, file5=null;
		int picNum = 0;
		if (InImg1 != null) {
			picNum++;
			CreatePic(InImg1);
			File file = new File(InImg1); 
			file1 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
		}
		if (InImg2 != null) {
			picNum++;
			CreatePic(InImg2);
			File file = new File(InImg2); 
			file2 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
		}
		if (InImg3 != null) {
			picNum++;
			CreatePic(InImg3);
			File file = new File(InImg3); 
			file3 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
			
		}
		if (InImg4 != null) {
			picNum++;
			CreatePic(InImg4);
			File file = new File(InImg4); 
			file4 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
		}
		if (InImg5 != null) {
			picNum++;
			CreatePic(InImg5);
			File file = new File(InImg5); 
			file5 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
		}

		try {
			Image InImage1 = ImageIO.read(file1);
			int width = InImage1.getWidth(null);
			int height = InImage1.getHeight(null);
			bufferedImage = new BufferedImage(width*picNum, height, BufferedImage.TYPE_INT_RGB);
			graphics = bufferedImage.createGraphics();	
			graphics.drawImage(InImage1, 0, 0, null);
			
		    if (file2 != null){
			Image InImage2 = ImageIO.read(file2);
			graphics.drawImage(InImage2, width*1, 0, null);
		    }
			
		    if (file3 != null){
			Image InImage3 = ImageIO.read(file3);
			graphics.drawImage(InImage3, width*2, 0, null);
		    }
		    
		    if (file4 != null){
				Image InImage4 = ImageIO.read(file4);
				graphics.drawImage(InImage4, width*3, 0, null);
			    }
		    
		    if (file5 != null){
				Image InImage5 = ImageIO.read(file5);
				graphics.drawImage(InImage5, width*4, 0, null);
			    }
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		graphics.dispose();
		try {
			String name = file1.getName();
			if (file2 != null){
				name += file2.getName();
			}
			if (file3 != null){
				name += file3.getName();
			}
			if (file4 != null){
				name += file4.getName();
			}
			if (file5 != null){
				name += file5.getName();
			}
			
			FileOutputStream outImage = new FileOutputStream(
					file1.getParentFile() + "/newx/" + name);
			ImageIO.write(bufferedImage, "jpg", outImage);
			outImage.close();
			System.out.println("生成合并文件"+name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
}


import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifDirectoryBase;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifInteropDirectory;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.exif.ExifThumbnailDirectory;
import com.drew.metadata.jpeg.JpegDirectory;


public class PictureToolbak {


	public static void main2(String[] args) {
		//CreatePic("d:/test/aa.jpg");
		//CreatePics("d:/test/M98 (1).jpg","d:/test/ma (1).jpg", "d:/test/Y15 (1).jpg",null, null);
		//CreatePics("d:/test/M98 (2).jpg","d:/test/ma (2).jpg", "d:/test/Y15 (2).jpg",null, null);
		//CreatePics("d:/test/(1)iphone .JPG","d:/test/(1)M98 .jpg", "d:/test/(1)ma .jpg","d:/test/(1)Y15 .jpg", null);
		//String[] baseDirs = new String[5];
/*		
		File directory1 = new File(new File("").getAbsolutePath());//设定为当前文件夹
		try {
			System.out.println(directory1.getCanonicalPath());
		    System.out.println(directory1.getAbsolutePath());//获取绝对路径
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//获取标准的路径

		//directory1 = new File(directory1.getAbsolutePath());
		File[] childFiles1=directory1.listFiles();
		//找出所有子目录 
		for(int i=0; childFiles1!=null && i < childFiles1.length; i++)
		{ 
			System.out.println("childFiles1[i]="+childFiles1[i].getPath());
			if(childFiles1[i].isDirectory())
			{
				System.out.println("childFiles1[i]="+childFiles1[i].getPath());
			} 
		}
*/

		List[] resultList = new List[5];
		int k = 0;
		if (args.length == 0) {
			File directory = new File(System.getProperty("user.dir"));//设定为当前文件夹
			File[] childFiles=directory.listFiles();
			//找出所有子目录 
			System.out.println("directory="+directory.getPath());
			for(int i=0; childFiles!=null && i<childFiles.length;i++)  
			{ 
				if(childFiles[i].isDirectory() && childFiles[i].getPath().indexOf("newx") == -1)
				{
					System.out.println("childFiles1[i]="+i+childFiles[i].getPath());
					String fileName = "*.jpg";
					resultList[k] = new ArrayList();
					FileSearcher.findFiles(childFiles[i].getPath(), fileName, resultList[k]);
					if (resultList[k].size() == 0) {
						System.out.println("No File Fount.");
					} else {
						for (int j = 0; j < resultList[k].size(); j++) {
							System.out.println(resultList[k].get(j));// 显示查找结果。
						}
					}
					k++;
				} 
			}
		} else {

			for (int i = 0; i < args.length; i++) {
				System.out.println(args[i]);
				// baseDirs[i] = args[i];
				String fileName = "*.jpg";
				resultList[i] = new ArrayList();
				FileSearcher.findFiles(args[i], fileName, resultList[i]);
				if (resultList[i].size() == 0) {
					System.out.println("No File Fount.");
				} else {
					for (int j = 0; j < resultList[i].size(); j++) {
						System.out.println(resultList[i].get(j));// 显示查找结果。
					}
				}
			}
		}
		if (resultList[0] != null) {
			for (int i = 0; i < resultList[0].size(); i++) {
				CreatePics(
						resultList[0]!=null?((String) resultList[0].get(i).toString()).replaceAll("\\\\", "/"):null, 
						resultList[1]!=null?((String) resultList[1].get(i).toString()).replaceAll("\\\\", "/"):null,
						resultList[2]!=null?((String) resultList[2].get(i).toString()).replaceAll("\\\\", "/"):null, 
						resultList[3]!=null?((String) resultList[3].get(i).toString()).replaceAll("\\\\", "/"):null, 
						resultList[4]!=null?((String) resultList[4].get(i).toString()).replaceAll("\\\\", "/"):null);
			}
		}
/*		
        String baseDIR = "D:/Pictures - 副本/iPhone 6s";    
        String fileName = "*.jpg";    
        List resultList = new ArrayList();   
        FileSearcher.findFiles(baseDIR, fileName, resultList);    
        if (resultList.size() == 0) {   
            System.out.println("No File Fount.");   
        } else {   
            for (int i = 0; i < resultList.size(); i++) {   
                System.out.println(resultList.get(i));//显示查找结果。    
            }   
        }   
        

        String baseDIR1 = "D:/Pictures - 副本/M98";    
        //    找扩展名为txt的文件    
        List resultList1 = new ArrayList();   
        FileSearcher.findFiles(baseDIR1, fileName, resultList1);    
        if (resultList1.size() == 0) {   
            System.out.println("No File Fount.");   
        } else {   
            for (int i = 0; i < resultList1.size(); i++) {   
                System.out.println(resultList1.get(i));//显示查找结果。    
            }   
        }   
        

        String baseDIR2 = "D:/Pictures - 副本/MA01";    
        //    找扩展名为txt的文件     
        List resultList2 = new ArrayList();   
        FileSearcher.findFiles(baseDIR2, fileName, resultList2);    
        if (resultList2.size() == 0) {   
            System.out.println("No File Fount.");   
        } else {   
            for (int i = 0; i < resultList2.size(); i++) {   
                System.out.println(resultList2.get(i));//显示查找结果。    
            }   
        }   
        

        String baseDIR3 = "D:/Pictures - 副本/Y15_1";    
        //    找扩展名为txt的文件   
        List resultList3 = new ArrayList();   
        FileSearcher.findFiles(baseDIR3, fileName, resultList3);    
        if (resultList3.size() == 0) {   
            System.out.println("No File Fount.");   
        } else {   
            for (int i = 0; i < resultList3.size(); i++) {   
                System.out.println(resultList3.get(i));//显示查找结果。    
            }   
        }   
        //String s = ((String)resultList.get(0).toString()).replaceAll("\\\\","/");
        //System.out.println(s);//显示查找结果。
        for (int i= 0; i < resultList.size(); i++){
        	CreatePics(((String)resultList.get(i).toString()).replaceAll("\\\\", "/"),((String)resultList1.get(i).toString()).replaceAll("\\\\", "/"), 
        			((String)resultList2.get(i).toString()).replaceAll("\\\\", "/"),((String)resultList3.get(i).toString()).replaceAll("\\\\", "/"), null);
        }
*/
        
	}

	static String s_info = ""; 
	static String fontName = "宋体";
	static int fontStyle = Font.BOLD; 
	static int fontSize = 18;
	static Color fontColor = Color.GREEN;
	static int fontX = 10;
	static int fontY = 10+fontSize;
	static float fontAlpha = 1.0f;
	static Graphics2D graphics = null;
	static BufferedImage bufferedImage = null;
	static int linenum = 5;
	public static void CreatePic(String InImg){
		File file = new File(InImg);
		try {
			Image InImage = ImageIO.read(file);
			int width = InImage.getWidth(null);
			int height = InImage.getHeight(null);
			// 将目标图片加载到内存。
			int w = (int)(width*0.5f);
			int h = (int)(height*0.5f);
			bufferedImage = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
			graphics = bufferedImage.createGraphics();
			AffineTransform origXform = graphics.getTransform();
		    AffineTransform newXform = (AffineTransform)(origXform.clone());
		    // center of rotation is center of the panel
		    newXform.setToScale(0.5f, 0.5f);
		    graphics.setTransform(newXform); 
			graphics.drawImage(InImage, 0, 0, null);
			// Reset to Original
			graphics.setTransform(origXform);
			
			//graphics.drawImage(InImage, 0, 0, width, height, null);
			graphics.setFont(new Font(fontName, fontStyle, fontSize));
			graphics.setColor(fontColor);
			// 设置水印图片的透明度。
			graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
					fontAlpha));
	
			HistogramAnalysisAlg his = new HistogramAnalysisAlg(bufferedImage);
			AffineTransform at = new AffineTransform();
	        at.scale(0.5f, 0.25f);
	        BufferedImageOp bi = new AffineTransformOp(at, null);
			graphics.drawImage(his.getHistogram(), bi, 5, 5);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
        Metadata metadata;
        try {
        	String s = file.getName();
        	
            if (s != null){
            	s_info = "名字:"+s;
            	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                linenum++;
            }
            
           
			metadata = ImageMetadataReader.readMetadata(file);
			for (Directory directory : metadata.getDirectories()) {
				
				s = directory.getString(ExifIFD0Directory.TAG_MAKE);
                if (s != null){
                	s_info = "厂商:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifIFD0Directory.TAG_SOFTWARE);
                if (s != null){
                	s_info = "软件:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                
                s = directory.getString(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH);
                if (s != null){
                	s_info = "宽度:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT);
                if (s != null){
                	s_info = "高度:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                
                s = directory.getString(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
                if (s != null){
                	s_info = "日期时间:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_TIME);
                if (s != null){
                	/*
                	String[] ss= s.split("/");
                	float s1=Float.valueOf(ss[0]);
                	float s2=Float.valueOf(ss[1]);
                	int s3=Math.round(s2/s1);
                	System.out.println("1/"+s3);
                	*/
                	s_info = "曝光时间:"+ directory.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_TIME);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_BIAS);
                if (s != null){
                	s_info = "曝光补偿:"+directory.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_BIAS);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_MODE);
                if (s != null){
                	s_info = "曝光模式:"+directory.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_MODE);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_FNUMBER);
                if (s != null){
                	s_info = "光圈:"+ directory.getDescription(ExifSubIFDDirectory.TAG_FNUMBER);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getDescription(ExifSubIFDDirectory.TAG_MAX_APERTURE);//directory.getString(ExifSubIFDDirectory.TAG_MAX_APERTURE);
                if (s != null){
                	s_info = "最大光圈:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT);
                if (s != null){
                	s_info = "ISO:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_FLASH);
                if (s != null){
                	s_info = "闪光灯:"+directory.getDescription(ExifSubIFDDirectory.TAG_FLASH);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_FOCAL_LENGTH);
                if (s != null){
                	s_info = "焦距:"+s;
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_METERING_MODE);
                if (s != null){
                	s_info = "测光模式:"+directory.getDescription(ExifSubIFDDirectory.TAG_METERING_MODE);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                s = directory.getString(ExifSubIFDDirectory.TAG_WHITE_BALANCE_MODE);
                if (s != null){
                	s_info = "白平衡:"+directory.getDescription(ExifSubIFDDirectory.TAG_WHITE_BALANCE_MODE);
                	graphics.drawString(s_info, fontX, fontY+linenum*fontSize);
                    linenum++;
                }
                
                
			}
		} catch (ImageProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        graphics.dispose();
        linenum = 5;
		try {
			FileOutputStream outImage = new FileOutputStream(file.getParentFile().getParentFile()+"/new"+file.getName());
			ImageIO.write(bufferedImage, "jpg", outImage);
			outImage.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public static void CreatePics(String InImg1, String InImg2, String InImg3,
			String InImg4, String InImg5) {
		File file1 = null, file2 = null, file3=null, file4 = null, file5=null;
		int picNum = 0;
		if (InImg1 != null) {
			picNum++;
			CreatePic(InImg1);
			File file = new File(InImg1); 
			file1 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
		}
		if (InImg2 != null) {
			picNum++;
			CreatePic(InImg2);
			File file = new File(InImg2); 
			file2 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
		}
		if (InImg3 != null) {
			picNum++;
			CreatePic(InImg3);
			File file = new File(InImg3); 
			file3 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
			
		}
		if (InImg4 != null) {
			picNum++;
			CreatePic(InImg4);
			File file = new File(InImg4); 
			file4 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
		}
		if (InImg5 != null) {
			picNum++;
			CreatePic(InImg5);
			File file = new File(InImg5); 
			file5 = new File(file.getParentFile().getParentFile()+"/new"+file.getName());
		}

		try {
			Image InImage1 = ImageIO.read(file1);
			int width = InImage1.getWidth(null);
			int height = InImage1.getHeight(null);
			bufferedImage = new BufferedImage(width*picNum, height, BufferedImage.TYPE_INT_RGB);
			graphics = bufferedImage.createGraphics();	
			graphics.drawImage(InImage1, 0, 0, null);
			
		    if (file2 != null){
			Image InImage2 = ImageIO.read(file2);
			graphics.drawImage(InImage2, width*1, 0, null);
		    }
			
		    if (file3 != null){
			Image InImage3 = ImageIO.read(file3);
			graphics.drawImage(InImage3, width*2, 0, null);
		    }
		    
		    if (file4 != null){
				Image InImage4 = ImageIO.read(file4);
				graphics.drawImage(InImage4, width*3, 0, null);
			    }
		    
		    if (file5 != null){
				Image InImage5 = ImageIO.read(file5);
				graphics.drawImage(InImage5, width*4, 0, null);
			    }
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		graphics.dispose();
		try {
			String name = file1.getName();
			if (file2 != null){
				name += file2.getName();
			}
			if (file3 != null){
				name += file3.getName();
			}
			if (file4 != null){
				name += file4.getName();
			}
			if (file5 != null){
				name += file5.getName();
			}
			
			FileOutputStream outImage = new FileOutputStream(
					file1.getParentFile() + "/newx/" + name);
			ImageIO.write(bufferedImage, "jpg", outImage);
			outImage.close();
			System.out.println("生成合并文件"+name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
}


猜你喜欢

转载自blog.csdn.net/blogercn/article/details/78355682