图像与数组转换

package com.prj.controller;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ImageWithArray {
 
 public static void main(String []args){
  //读取图片到BufferImage
  BufferedImage bf = readImage("D:\easy-shopping\WebContent\upload\image\ad_login.jpg");//读取绝对路径+文件名
  //Image转化二维数组
  int[][] rgbArray1 = convertImageToArray(bf);
  //输出图片到指定文件
  writeImageFromArray("D:\IDEA\2.jpg","jpg",rgbArray1);//输出绝对路径+文件名
  
  System.out.println("图片输出完毕!");
 }
 private static BufferedImage readImage(String imageFile) {
  File file= new File(imageFile);
  BufferedImage bf = null;
  try{
   bf =ImageIO.read(file);
  }catch (Exception e) {
   e.printStackTrace();
  }
  return bf;
 }
 
 private 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;
 }

 private static void writeImageFromArray(String imageFile, String type,int[][] rgbArray) {
  //获取数组宽度和高度
  int width = rgbArray[0].length;
  int height = rgbArray.length;
  //将二维数组转换一维数组
  int [] data = new int[width*height];
  for(int i=0;i<height;i++)
  for(int j=0;j<height;j++)
   data[i*width+j]=rgbArray[i][j];
  //将数据写入BufferImage
  BufferedImage bf = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
  bf.setRGB(0, 0, width, height, data, 0, width);
  //输出图片
  try{
   File file = new File(imageFile);
   ImageIO.write((BufferedImage)bf, type, file);
  }catch (Exception e) {
   e.printStackTrace();
  }
 }
 
}

作者:KeerDi —— 北方的后生

出处:http://www.cnblogs.com/keerdi/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/123hll/p/9066878.html