---
title: "设定PNG的bKGD"
author: "yutiansky"
date: "2012-06-01T17:20:33Z"
tags: []
categories: ["编程语言"]
slug: "png-bkgd"
draft: false
lastmod: "2012-06-01T17:20:33Z"
id: 1546738
url: "https://dncsoft.iteye.com/blog/1546738"
---

一，参考资料

 PNG结构说明 [http://homepage2.nifty.com/sophia0/png.html](http://homepage2.nifty.com/sophia0/png.html)

 JavaSample [http://www.java.net/node/707282](http://www.java.net/node/707282)

 JavaSource [http://www.docjar.com/html/api/com/sun/imageio/plugins/png/PNGMetadata.java.html](http://www.docjar.com/html/api/com/sun/imageio/plugins/png/PNGMetadata.java.html)

 

二，JavaSample

```
    private void writeImageLocal(final String fileName, final BufferedImage image) throws Exception {

        if (fileName == null || "".equals(fileName.trim())) {
            throw new Exception("fileName is null or empty");
        }
        if (image == null) {
            throw new Exception("BufferedImage is null");
        }

        PNGMetadata png = new PNGMetadata();
        png.bKGD_present = true;
        png.bKGD_colorType = 2;
        png.bKGD_red = 123;
        png.bKGD_green = 123;
        png.bKGD_blue = 123;
        
        IIOImage iioImage = new IIOImage(image, null, png);
        
        ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();
        writer.setOutput(ImageIO.createImageOutputStream(new File(fileName)));
        writer.write(iioImage);
        writer.dispose();
    }
```

 

三，链接中有一个查看PNG结构的工具。

 

 

// END