Java imageio图片处理

暑假就快要结束了,整理一下自己所学的

 

RGB565->RGB888

red = ( color & 0xf800 ) >> 8;
green = ( color & 0x07e0 ) >> 3;
blue = (color &0x001f )  << 3;

color_rgb888 = (red<<16) + (green<<8) + (blue<<0);

 

压缩图片

package cc;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;

public class ImageUtil {

	public static void main(String[] args) throws IOException {

		String str = "C:\\Users\\fuchen\\Desktop\\timg.jpg"; // 需要压缩图片的地址
		String str1 = "C:\\Users\\fuchen\\Desktop\\z.jpg"; // 压缩后图片的地址

		boolean flag = compressPic(str, str1);
		if (flag) {
			System.out.println("成功");
		} else {
			System.out.println("失败");
		}
	}

	// 需要压缩的图片的路径,压缩后生产的图片的路径
	public static boolean compressPic(String srcFilePath, String descFilePath) throws IOException {

		File file = null;
		BufferedImage src = null;
		FileOutputStream out = null;
		ImageWriter imgWrier;
		ImageWriteParam imgWriteParams;

		// 指定写图片的方式为 jpg
		imgWrier = ImageIO.getImageWritersByFormatName("jpg").next();
		imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(null);
		// 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
		imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
		// 这里指定压缩的程度,参数qality是取值0~1范围内,
		imgWriteParams.setCompressionQuality((float) 0.1);
		imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
		ColorModel colorModel = ImageIO.read(new File(srcFilePath)).getColorModel();// ColorModel.getRGBdefault();
		// 指定压缩时使用的色彩模式
//        imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier( 
//                colorModel, colorModel.createCompatibleSampleModel(16, 16))); 
		imgWriteParams.setDestinationType(
				new javax.imageio.ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
		try {
			if (isBlank(srcFilePath)) {
				return false;
			} else {
				file = new File(srcFilePath);
				System.out.println(file.length());
				src = ImageIO.read(file); // 我去将它转化为 二维数组

				/*
				 * // 将图片转换为二维数组 int[][] rgbArray1 = convertImageToArray(src); // 输出图片到指定文件
				 * writeImageFromArray("C:\\Users\\fuchen\\Desktop\\转化成功.jpg", "jpg",
				 * rgbArray1);// 这里写你要输出的绝对路径+文件名 System.out.println("图片输出完毕!");
				 */

				out = new FileOutputStream(descFilePath);
				imgWrier.reset();
				// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
				// OutputStream构造
				imgWrier.setOutput(ImageIO.createImageOutputStream(out));
				// 调用write方法,就可以向输入流写图片
				imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams);
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean isBlank(String string) {
		if (string == null || string.length() == 0 || string.trim().equals("")) {
			return true;
		}
		return false;
	}

	// BufferedImage所读取的图片文件转化为像素数组
	public static int[][] convertImageToArray(BufferedImage bf) {
		// 获取图片宽度和高度
		int width = bf.getWidth();
		int height = bf.getHeight();
		// 将图片sRGB数据写入一维数组
		int[] data = new int[width * height];

		bf.getRGB(0, 0, width, height, data, 0, width);

		// 将一维数组转换为为二维数组
		int[][] rgbArray = new int[height][width];
		for (int i = 0; i < height; i++)
			for (int j = 0; j < width; j++)
				rgbArray[i][j] = data[i * width + j];
		return rgbArray;
	}

}

 

 

复制图片

package cc;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;

public class zz {
	public static void main(String[] args) throws FileNotFoundException {
		// 读取图片到BufferedImage
		BufferedImage bf = readImage("C:\\Users\\fuchen\\Desktop\\timg.jpg");// 这里写你要读取的绝对路径+文件名

		System.out.println(bf.toString());
		// 将图片转换为二维数组
		int[][] rgbArray1 = convertImageToArray(bf);

		// 输出图片到指定文件
		writeImageFromArray("C:\\Users\\fuchen\\Desktop\\转化成功zz.jpg", "jpg", rgbArray1);// 这里写你要输出的绝对路径+文件名
		System.out.println("图片输出完毕!");
	}

	public static BufferedImage readImage(String imageFile) {
		File file = new File(imageFile);
		BufferedImage bf = null;
		try {
			bf = ImageIO.read(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bf;
	}

	public static int[][] convertImageToArray(BufferedImage bf) {
		// 获取图片宽度和高度
		int width = bf.getWidth();
		int height = bf.getHeight();
		// 将图片sRGB数据写入一维数组
		int[] data = new int[width * height];

		bf.getRGB(0, 0, width, height, data, 0, width);

		// 将一维数组转换为为二维数组
		int[][] rgbArray = new int[height][width];
		for (int i = 0; i < height; i++)
			for (int j = 0; j < width; j++)
				rgbArray[i][j] = data[i * width + j];
		return rgbArray;
	}

	public static void writeImageFromArray(String imageFile, String type, int[][] rgbArray)
			throws FileNotFoundException {
		// 获取数组宽度和高度
		int width = rgbArray[0].length;
		int height = rgbArray.length;
		System.out.println("width = " + width + " height = " + height);

		int[] data = new int[width * height];
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				data[i * width + j] = rgbArray[i][j];
				data[i * width + j] = data[i * width + j];
			}
		}

		// 将数据写入BufferedImage
		BufferedImage bf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		bf.setRGB(0, 0, width, height, data, 0, width);
		// 输出图片
		try {
			File file = new File(imageFile);
			ImageIO.write((RenderedImage) bf, type, file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 

你可能感兴趣的:(Java图片处理)