首页 / JAVA / java中图像与数组转换
java中图像与数组转换
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java中图像与数组转换,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1790字,纯文字阅读大概需要3分钟。
内容图文
import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageWithArray { public static void main(String[] args) { // 读取图片到BufferedImage BufferedImage bf = readImage("c:\\tmp\\6\\female.png");//这里写你要读取的绝对路径+文件名 // 将图片转换为二维数组int[][] rgbArray1 = convertImageToArray(bf); // 输出图片到指定文件 writeImageFromArray("c:\\tmp\\2.png", "png", rgbArray1);//这里写你要输出的绝对路径+文件名 System.out.println("图片输出完毕!"); } publicstatic BufferedImage readImage(String imageFile){ File file = new File(imageFile); BufferedImage bf = null; try { bf = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); } return bf; } publicstaticint[][] convertImageToArray(BufferedImage bf) { // 获取图片宽度和高度int width = bf.getWidth(); int height = bf.getHeight(); // 将图片sRGB数据写入一维数组int[] data = newint[width*height]; bf.getRGB(0, 0, width, height, data, 0, width); // 将一维数组转换为为二维数组int[][] rgbArray = newint[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; } publicstaticvoid writeImageFromArray(String imageFile, String type, int[][] rgbArray){ // 获取数组宽度和高度int width = rgbArray[0].length; int height = rgbArray.length; // 将二维数组转换为一维数组int[] data = newint[width*height]; for(int i = 0; i < height; i++) for(int j = 0; j < width; j++) data[i*width + j] = rgbArray[i][j]; // 将数据写入BufferedImage 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((RenderedImage)bf, type, file); } catch (IOException e) { e.printStackTrace(); } } }
原文:https://www.cnblogs.com/zhaohz/p/12117153.html
内容总结
以上是互联网集市为您收集整理的java中图像与数组转换全部内容,希望文章能够帮你解决java中图像与数组转换所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。