栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何用BufferedImage中的另一种颜色替换颜色

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何用BufferedImage中的另一种颜色替换颜色

为了避免遍历像素,请更改基础ColorModel。这是一个例子。以下是作者使用原始BufferedImage并应用新颜色模型的代码段。

 private static BufferedImage createImage() {    int width = 200;    int height = 200;    // Generate the source pixels for our image    // Lets just keep it to a simple blank image for now    byte[] pixels = new byte[width * height];    DataBuffer dataBuffer = new DataBufferByte(pixels, width*height, 0);    SampleModel sampleModel = new SinglePixelPackedSampleModel(    DataBuffer.TYPE_BYTE, width, height, new int[] {(byte)0xf});    WritableRaster raster = Raster.createWritableRaster(    sampleModel, dataBuffer, null);    return new BufferedImage(createColorModel(0), raster, false, null);}private static ColorModel createColorModel(int n) {    // Create a simple color model with all values mapping to    // a single shade of gray    // nb. this could be improved by reusing the byte arrays    byte[] r = new byte[16];    byte[] g = new byte[16];    byte[] b = new byte[16];    for (int i = 0; i < r.length; i++) {        r[i] = (byte) n;        g[i] = (byte) n;        b[i] = (byte) n;    }    return new IndexColorModel(4, 16, r, g, b);}private BufferedImage image = createImage();image = new BufferedImage(createColorModel(e.getX()), image.getRaster(), false, null);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407186.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号