使用Java进行图片底片化效果处理
使用java代码读取图片,并进行底片化处理
util
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; public static class FolderFileScanner { private static ArrayList<Object> scanFiles = new ArrayList<Object>(); public static ArrayList<Object> scanFilesWithRecursion(String folderPath){ ArrayList<String> dirctorys = new ArrayList<String>(); 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<forY;y++) { for(int x=0;x<forX;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<tempZoomEndY;yy++) { for(int xx=tempZoomStartX;xx<tempZoomEndX;xx++) { imageChangeRGB[yy*width+xx]=rgb; } } } } createImage.setRGB(0, 0, width, height, imageChangeRGB, 0, width); imageRGB=null; imageChangeRGB=null; } return createImage; } //图片反色处理 public static BufferedImage 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=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); for(int i=0;i<imageRGB.length;i++) { imageRGB[i]=imageRGB[i]^0xffffffff; } createImage.setRGB(0, 0, width, height, imageRGB, 0, width); imageRGB=null; } return createImage; } }
test
import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Date; import javax.imageio.ImageIO; import javax.swing.JFrame; import java.io.File; import java.util.ArrayList; import java.util.LinkedList; public class test extends JFrame{ private static final long serialVersionUID = 1L; BufferedImage image2=null; public test() { ArrayList<Object> 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=new File(path); File file2=new File(pathsub+ "-1.png"); Date d; long startTime; long endTime; float xZoom=20f; float yZoom=20f; try { BufferedImage image=ImageIO.read(file); //获取缩放并且反色后的图片 d=new Date(); startTime=d.getTime(); //图片反色后再按比例缩放 image2=ImageZoom.createZoomImage(ImageZoom.inverse(image),xZoom,yZoom); d=new Date(); 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 void paint(Graphics g){ g.drawImage(image2, 0, 30, this); } public static void main(String[] arg) { new test(); } }
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;
public static class FolderFileScanner {
private static ArrayList<Object> scanFiles = new ArrayList<Object>();
public static ArrayList<Object> scanFilesWithRecursion(String folderPath){
ArrayList<String> dirctorys = new ArrayList<String>();
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<forY;y++){for(int x=0;x<forX;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<tempZoomEndY;yy++){for(int xx=tempZoomStartX;xx<tempZoomEndX;xx++){imageChangeRGB[yy*width+xx]=rgb;}}}}createImage.setRGB(0, 0, width, height, imageChangeRGB, 0, width);imageRGB=null;imageChangeRGB=null;}return createImage;}//图片反色处理public static BufferedImage 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=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);for(int i=0;i<imageRGB.length;i++){imageRGB[i]=imageRGB[i]^0xffffffff;}createImage.setRGB(0, 0, width, height, imageRGB, 0, width);imageRGB=null;}return createImage;}}