关闭

java 写的简单处理图片程序

464人阅读 评论(0) 收藏 举报

还在实验中 没什么值得看的

Importing list from java.awt or java.utils
http://stackoverflow.com/questions/14163687/importing-list-from-java-awt-or-java-utils
try 后面要接catch 或者finally

读取文件夹下所有文件名
File file = new File(path);
File[] fileList = file.list();
File[] fileList = file.listFiles();
有嵌套用listFiles()
没嵌套用list()
String[] list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
File[] listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.


读入图片的几种方法


package picture;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

public class main {

	 public static void main(String[] args) throws IOException{
		ImageUtil utl = new ImageUtil();
		File imageFile = new File("c:\\Users\\wk\\Pictures\\MP Navigator EX\\2013_12_03\\IMG_0001.jpg");  
		Image image = ImageIO.read(imageFile);
		
		InputStream is = new BufferedInputStream(new FileInputStream("c:\\Users\\wk\\Pictures\\MP Navigator EX\\2013_12_03\\IMG_0001.jpg")); 
		image = ImageIO.read(is);
		OutputStream os = new BufferedOutputStream(new FileOutputStream("c:\\Users\\wk\\Pictures\\MP Navigator EX\\2013_12_03\\test.jpg"));  
		image = ImageIO.read(is);
		

		
		try {
		//从特定文件载入
			BufferedImage bi = ImageIO.read(new File("c:\\Users\\wk\\Pictures\\MP Navigator EX\\2013_12_03\\IMG_0001.jpg"));
			bi.getSubimage(0, 0, 10, 10);
			BufferedImage res;
			res = utl.resize(bi, 100, 100);
			ImageIO.write(res, "jepg", os);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

修改后的main

	 public static void main(String[] args) throws Exception{
	
		 //read file
		 fileread fr = new fileread();
		 //file name store in file List
		 List<String> fileList = new ArrayList<String>();
		 fileList = fr.readFile("c:\\Users\\wk\\Pictures\\MP Navigator EX\\2013_12_03\\");
		 
		 for (Iterator<String> iter = fileList.iterator(); iter.hasNext(); ){
			 String temp = iter.next();
			 ImageUtils utl = new ImageUtilsImpl();
			 InputStream is = new BufferedInputStream(new FileInputStream(temp));
			 OutputStream os = new BufferedOutputStream(new FileOutputStream(temp.replace(".jpg", "_cut.jpg")));
				try {

					utl.sub(1240, 1753, is, os,"jpeg");
					System.out.println("Picture"+temp.substring(30));

				} catch (IOException e) {
					e.printStackTrace();
				}
				
				os.close();
			}
		 System.out.print("end");
		 }


截取图片的几种方法

package picture;	
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

/**
 * 
 * @author wk
 * @version 1.0
 */
public abstract class ImageUtils {

	public static boolean sub(int width, int height, InputStream input,
			OutputStream output, String format) throws Exception {
		// 输入
		BufferedImage inputImage = ImageIO.read(input);
		// 截图
		Rectangle re = new Rectangle(10, 10, 1200, 1623);
		BufferedImage subImage = inputImage.getSubimage(
				re.x, re.y,
				re.width, re.height);
		
		// 初始化输出图片
		BufferedImage result = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		// 重新绘图
		Image image = subImage.getScaledInstance(result.getWidth(), result
				.getHeight(), result.getType());

		result.createGraphics().drawImage(image, null, null);
		RenderedImage im = (RenderedImage) result;
		// 输出
		return ImageIO.write(im, format, output);
	}

}


读入文件夹图片

package picture;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class fileread {

	public List<String> readFile(String path){
		List<String> result = new ArrayList<String>();
		File file = new File(path);
		file.list();
		File[] fileList = file.listFiles();
		
        for(int i=0;i<fileList.length;i++){ 
            result.add(fileList[i].getAbsolutePath());
        }	
        
		return result;
	}
}




   
0
0

猜你在找
【直播】计算机视觉原理及实战—屈教授
【套餐】深度学习入门视频课程—唐宇迪
【套餐】Hadoop生态系统零基础入门--侯勇蛟
【套餐】嵌入式Linux C编程基础--朱有鹏
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】机器学习之凸优化——马博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1384次
    • 积分:29
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档