一、得到图片中的数据

1、现有一副png图片,提取其中某个区域的内容,保存成另外的图片。

处理后云图如下,黑色的表示云。

现在要得到河南省的的,比如区域的范围是(270,270)到(390,390)的区域。

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GraphicsConfiguration;

import java.awt.GraphicsDevice;

import java.awt.GraphicsEnvironment;

import java.awt.HeadlessException;

import java.awt.Image;

import java.awt.Transparency;

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;

import java.awt.image.PixelGrabber;

import java.io.File;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

public class LoadOneImage {

public static void main(String[] args) {

String myreadline = "";

// 定义一个String类型的变量,用来每次读取一行

try {

myreadline = "mask_cloud.png";// 读取一行

BufferedImage image = toBufferedImage(new ImageIcon("data/Image/"

+ myreadline).getImage());

int height = image.getHeight();

int width = image.getWidth();

System.out.println("图片的高度为------>" + height);

System.out.println("图片的宽度为------>" + width);

// 创建BufferedImage对象

BufferedImage targetimage = new BufferedImage(1280, 1024,BufferedImage.TYPE_INT_RGB);

// 获取Graphics2D

Graphics2D g2d = targetimage.createGraphics();

// ---------- 增加下面的代码使得背景透明 -----------------

targetimage = g2d.getDeviceConfiguration().createCompatibleImage(

1280, 1024, Transparency.TRANSLUCENT);

// 释放对象

g2d.dispose();

g2d = targetimage.createGraphics();

// ---------- 背景透明代码结束 -----------------

for (int y = 270; y < 390; y++) {

for (int x = 270; x < 390; x++) {

Color color = new Color(image.getRGB(x, y));

if (color.equals(Color.BLACK))

g2d.setColor(Color.RED);

else

g2d.setColor(Color.BLUE);

g2d.setStroke(new BasicStroke(1));

g2d.drawLine(x, y, x, y);

}

}

// 释放对象

g2d.dispose();

ImageIO.write(targetimage, "png", new File("test.png"));

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("结束");

}

// This method returns a buffered image with the contents of an image

public static BufferedImage toBufferedImage(Image image) {

if (image instanceof BufferedImage) {

return (BufferedImage) image;

}

// Determine if the image has transparent pixels; for this method's

// implementation, see e661 Determining If an Image Has Transparent

// Pixels

boolean hasAlpha = hasAlpha(image);

// Create a buffered image with a format that's compatible with the

// screen

BufferedImage bimage = null;

GraphicsEnvironment ge = GraphicsEnvironment

.getLocalGraphicsEnvironment();

try {

// Determine the type of transparency of the new buffered image

int transparency = Transparency.OPAQUE;

if (hasAlpha) {

transparency = Transparency.BITMASK;

}

// Create the buffered image

GraphicsDevice gs = ge.getDefaultScreenDevice();

GraphicsConfiguration gc = gs.getDefaultConfiguration();

bimage = gc.createCompatibleImage(image.getWidth(null), image

.getHeight(null), transparency);

} catch (HeadlessException e) {

// The system does not have a screen

}

if (bimage == null) {

// Create a buffered image using the default color model

int type = BufferedImage.TYPE_INT_RGB;

if (hasAlpha) {

type = BufferedImage.TYPE_INT_ARGB;

}

bimage = new BufferedImage(image.getWidth(null), image

.getHeight(null), type);

}

// Copy image to buffered image

Graphics g = bimage.createGraphics();

// Paint the image onto the buffered image

g.drawImage(image, 0, 0, null);

g.dispose();

return bimage;

}

// This method returns true if the specified image has transparent pixels

public static boolean hasAlpha(Image image) {

// If buffered image, the color model is readily available

if (image instanceof BufferedImage) {

BufferedImage bimage = (BufferedImage) image;

return bimage.getColorModel().hasAlpha();

}

// Use a pixel grabber to retrieve the image's color model;

// grabbing a single pixel is usually sufficient

PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);

try {

pg.grabPixels();

} catch (InterruptedException e) {

}

// Get the image's color model

ColorModel cm = pg.getColorModel();

return cm.hasAlpha();

}

}

结果如下:

其中黑色的用红色填充了,白色的用蓝色填充了。

java png图片读取_png图片的读取相关推荐

  1. java bmp_JAVA实现对BMP图片的读取

    BMP图片格式,是windows自带的一个图片格式,(*bmp),在windows的系统下都支持这种格式,bmp格式与设备无关的位图(DIB)格式,BMP简称位图,BMP的原始数据是没有经过压缩处理的 ...

  2. 使用javax.imageio.ImageIO读取JPEG图片时出现异常java.awt.color.CMMException: General CMM error517

    目录 问题描述 背景 异常 解决 重现 紧急处理 解决方法 第一种:变更JDK版本 第二种:去掉引入的twelvemonkeys图片读取插件 问题描述 背景 由于业务需要,生产环境需要将用户上传的图片 ...

  3. java读取本地图片及路径

    手贱打算用java读取本地图片,发现各种问题,之前使用python,发现还是python太厉害 如果打算用文件的形式读取图片,注意要对文件流做异常处理,还有java的输出语句,连接符之间的关系.最后成 ...

  4. opencv java 显示图片_【opencv三】利用opencv读取显示图片

    在opencv中读取显示图片的头文件是highgui.hpp. 整体代码如下,如要测试自己的图片,需要将代码段中的图片地址更改为自己图片的绝对路径. #include "opencv2/hi ...

  5. JAVA读取本地图片并展示

    代码如下: //读取本地图片输入流 FileInputStream inStream = new FileInputStream("D:/image/123.jpg");//byt ...

  6. java 读取数据库图片二进制流并输出到jsp页面

    最近刚好遇到这个问题,刚开始在网上各种搜索,看到大量有关该问题的博文. 大量文章有些不敢苟同,希望博主们要写就认真写,请不要浪费其他需要帮助的人的时间去验证你的博文是否正确. 正文如下: 流程说明: ...

  7. java 读取excel图片导入(亲测有效)

    从excel文件中获取图片(兼容新老版本) /*** 获取excel表中的图片** @return* @throws IOException* @throws InvalidFormatExcepti ...

  8. CocosCreator之KUOKUO带你搞反射-jsb读取相册图片

    摘要 在使用 CocosCreator 打包的原生应用中,我们可以通过引擎封装好的反射机制调用 Java 的静态方法,从而实现提示.相册.支付跳转等功能.本次内容为利用 jsb 读取相册图片. 正文 ...

  9. 基于springboot架构的读取excel 图片并自动上传

    基于springboot架构的读取excel 图片并自动上传 excel 图片上传 页面准备 comment.html 逻辑处理准备 控制类CommentController.java 接口类ICom ...

最新文章

  1. 冷热分离和直接使用大数据库_中台有“数”:大数据技术为苏宁818保驾护航
  2. 从零开始学_JavaScript_系列(14)——dojo(7)(饼图,BorderContainer,hashchange,弹窗)...
  3. 文档生产工具 Doxygen
  4. 微信小程序注册/登录接口开发
  5. spring IOC基本配置(xml配置和注解配置)
  6. android 点击空白退出,Android 点击空白处隐藏键盘
  7. 紧急预警:wls9_async_response.war组件漏洞的延续
  8. 我是如何看穿候选人伪装的项目经验的?
  9. 纤亿通教你如何选择合适的 Cat6 网线
  10. itext pdf 基本使用实战
  11. 机器学习之ROC曲线绘制
  12. Python - 多个Excel合并 (列不同序 或 列数不同)
  13. 广州图书馆跳转中国知网教程
  14. sel4 手册总结之介绍与内核服务和对象
  15. 上传大文件报错413问题处理
  16. python编程一个正方体的代码_Linux Shell经典面试题之请用shell或Python编写一个正方形(square.sh),接受用户输入的数字...
  17. 国内首部区块链行业纪录片开播
  18. asp.net面试题
  19. 这届年轻人,是最孤独的一代吗?
  20. CSS学习笔记——精灵图(sprite)

热门文章

  1. 图解事件坐标screenX、clientX、pageX, offsetX的区别
  2. tp5html的if判断,TP5 判断方法
  3. “谈了四年的男友寒心了,我已是接近30的老女人”
  4. split 自动分割文件脚本
  5. mysql8.0源码分析——文件管理fil_system
  6. Android 二维码扫描(仿微信界面),根据Google zxing
  7. 鸿蒙中国壁纸高清全面屏,华为P50pro最新确认:麒麟1020+立体全面屏+鸿蒙系统,这才是华为...
  8. LPC2141芯片解密 芯片解密干什么
  9. 微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
  10. 大风起兮云飞扬 —记2011年的中国云计算