使用java代码读取图片,并进行底片化处理
util
importjava.awt.image.BufferedImage;importjava.io.File;importjava.util.ArrayList;importjava.util.LinkedList;public classImageZoom{/****/
private static final long serialVersionUID = 1L;
public static classFolderFileScanner {private static ArrayList scanFiles = new ArrayList();public static ArrayListscanFilesWithRecursion(String folderPath){
ArrayList dirctorys = new ArrayList();
File directory= newFile(folderPath);if(!directory.isDirectory()){//throw new Exception('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^");
}if(directory.isDirectory()){
File [] filelist=directory.listFiles();for(int i = 0; i < filelist.length; i ++){/**如果当前是文件夹,进入递归扫描文件夹**/
if(filelist[i].isDirectory()){
dirctorys.add(filelist[i].getAbsolutePath());/**递归扫描下面的文件夹**/scanFilesWithRecursion(filelist[i].getAbsolutePath());
}/**非文件夹**/
else{
scanFiles.add(filelist[i].getAbsolutePath());
}
}
}returnscanFiles;
}
}privateImageZoom()
{}//图片按比例缩放
public static BufferedImage createZoomImage(BufferedImage image,floatzoom)
{returncreateZoomImage(image,zoom,zoom);
}//图片不按比例缩放
public static BufferedImage createZoomImage(BufferedImage image,float xZoom,floatyZoom)
{
BufferedImage createImage=null;if(image!=null)
{int width=0;int height=0;int forX=0;int forY=0;int tempZoomStartX=0;int tempZoomStartY=0;int tempZoomEndX=0;int tempZoomEndY=0;int rgb=0;int imageRGB[] = null;int imageChangeRGB[] = null;
width=(int)(image.getWidth()*xZoom);
height=(int)(image.getHeight()*yZoom);
forX=image.getWidth();
forY=image.getHeight();
imageChangeRGB=new int[height*width];
imageRGB=new int[forX*forY];
image.getRGB(0, 0, forX, forY, imageRGB, 0, forX);
createImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);for(int y=0;y
{for(int x=0;x
{
tempZoomStartX=(int)(x*xZoom);
tempZoomStartY=(int)(y*yZoom);
tempZoomEndX=(int)((x+1)*xZoom);
tempZoomEndY=(int)((y+1)*yZoom);
rgb=imageRGB[y*forX+x];for(int yy=tempZoomStartY;yy
{for(int xx=tempZoomStartX;xx
{
imageChangeRGB[yy*width+xx]=rgb;
}
}
}
}
createImage.setRGB(0, 0, width, height, imageChangeRGB, 0, width);
imageRGB=null;
imageChangeRGB=null;
}returncreateImage;
}//图片反色处理
public staticBufferedImage inverse(BufferedImage image)
{
BufferedImage createImage=null;if(image!=null)
{int width=0;int height=0;
width=image.getWidth();
height=image.getHeight();int imageRGB[]=new int[width*height];
image.getRGB(0, 0, width, height, imageRGB, 0, width);
createImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);for(int i=0;i
{
imageRGB[i]=imageRGB[i]^0xffffffff;
}
createImage.setRGB(0, 0, width, height, imageRGB, 0, width);
imageRGB=null;
}returncreateImage;
}
}
test
importjava.awt.Graphics;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjava.util.Date;importjavax.imageio.ImageIO;importjavax.swing.JFrame;importjava.io.File;importjava.util.ArrayList;importjava.util.LinkedList;public class test extendsJFrame{
private static final long serialVersionUID = 1L;
BufferedImage image2=null;publictest()
{
ArrayList scanFiles = ImageZoom.FolderFileScanner.scanFilesWithRecursion("C:\\\\Users\\\\Administrator\\\\Desktop");for (int i = 0; i < scanFiles.size(); i++) {
String path=scanFiles.get(i).toString();if (path.contains("-1.png")) {continue;
}
String pathsub= path.substring(0,path.length() - 4);
File file=newFile(path);
File file2=new File(pathsub+ "-1.png");
Date d;longstartTime;longendTime;float xZoom=20f;float yZoom=20f;try{
BufferedImage image=ImageIO.read(file);//获取缩放并且反色后的图片
d=newDate();
startTime=d.getTime();//图片反色后再按比例缩放
image2=ImageZoom.createZoomImage(ImageZoom.inverse(image),xZoom,yZoom);
d=newDate();
endTime=d.getTime();
System.out.println("使用时间:"+(endTime-startTime)+"毫秒");
ImageIO.write(image2,"png", file2);//保存转换后的图片
} catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}this.repaint();this.setBounds(0,0,600,500);this.setDefaultCloseOperation(3);this.setVisible(true);
file.delete();
}
}public voidpaint(Graphics g){
g.drawImage(image2,0, 30, this);
}public static voidmain(String[] arg)
{newtest();
}
}
import java.awt.image.BufferedImage;import java.io.File;import java.util.ArrayList;import java.util.LinkedList;
public class ImageZoom{/** * */private static final long serialVersionUID = 1L;/*** @author 邪恶小先生*/public static class FolderFileScanner {
private static ArrayList scanFiles = new ArrayList();
public static ArrayList scanFilesWithRecursion(String folderPath){ArrayList dirctorys = new ArrayList();File directory = new File(folderPath);if(!directory.isDirectory()){//throw new Exception('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^");}if(directory.isDirectory()){File [] filelist = directory.listFiles();for(int i = 0; i < filelist.length; i ++){/**如果当前是文件夹,进入递归扫描文件夹**/if(filelist[i].isDirectory()){dirctorys.add(filelist[i].getAbsolutePath());/**递归扫描下面的文件夹**/scanFilesWithRecursion(filelist[i].getAbsolutePath());}/**非文件夹**/else{scanFiles.add(filelist[i].getAbsolutePath());}}}return scanFiles;}}private ImageZoom(){}//图片按比例缩放public static BufferedImage createZoomImage(BufferedImage image,float zoom){return createZoomImage(image,zoom,zoom);}//图片不按比例缩放public static BufferedImage createZoomImage(BufferedImage image,float xZoom,float yZoom){BufferedImage createImage=null;if(image!=null){int width=0;int height=0;int forX=0;int forY=0;int tempZoomStartX=0;int tempZoomStartY=0;int tempZoomEndX=0;int tempZoomEndY=0;int rgb=0;int imageRGB[] = null;int imageChangeRGB[] = null;width=(int)(image.getWidth()*xZoom);height=(int)(image.getHeight()*yZoom);forX=image.getWidth();forY=image.getHeight();imageChangeRGB=new int[height*width];imageRGB=new int[forX*forY];image.getRGB(0, 0, forX, forY, imageRGB, 0, forX);createImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);for(int y=0;y