用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

海大软件1102班    -  云代码空间

—— 你究竟输入什么了导致它崩溃?

JAVA图形操作类

2012-12-31|2402阅||

摘要:  我们都知道,微软对其原有GDI(Graphics Device Interface)改进后形成了现在的GDI+。作为图形设备接口GDI的继任者,其对GDI的很多功能都进行了优化,而且还提供了许多方便实用的附加功能。  以此为基础,在.Net环境下可以很轻松的完成一系列复杂的图

  我们都知道,微软对其原有GDI(Graphics Device Interface)改进后形成了现在的GDI+。作为图形设备接口GDI的继任者,其对GDI的很多功能都进行了优化,而且还提供了许多方便实用的附加功能。

  以此为基础,在.Net环境下可以很轻松的完成一系列复杂的图形操作。而与之相较,Java的图像接口在图像处理功能上则略显单薄,而部分操作较之C#等还有些复杂。

  所以最近我打算在本人的loonframework项目中将一些java图像接口二次封装,及添加部分新的功能进入。下面我公布的,是一些在 Loonframework框架中将新添加的图像处理类。当然,这些还在测试阶段,最后的确定版估计会与此有较大出入,有改进意见的欢迎邮寄到我的 Email:ceponline@yahoo.com.cn。(最近挺懒的……一写代码就犯困……下班只看漫画度日……)

BaseImage.java:
package org.loon.framework.game.test.image;

import java.awt.Image;

/** *//**
 * <p>
 * Title: LoonFramework
 * </p>
 * <p>
 * Description:基础Bitmap接口
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: LoonFramework
 * </p>
 * 
 * @author chenpeng
 * @email:ceponline@yahoo.com.cn
 * @version 0.1
 */
public interface BaseImage ...{

    final static int BITMAPFILEHEADER_SIZE = 14;

    final static int BITMAPINFOHEADER_SIZE = 40;

    abstract int getWidth();

    abstract int getHeight();

    abstract LColor getPixel(int x, int y);

    /** *//**
     * 设定像素点
     * 
     * @param x
     * @param y
     * @param rgb
     * @return
     */
    abstract boolean setPixel(int x, int y, int rgb);

    abstract boolean setPixel(int x, int y, int r, int g, int b);

    abstract boolean setPixel(int x, int y, LColor color);

    /** *//**
     * 移动像素
     * 
     * @param dx
     * @param dy
     */
    abstract void movePixels(int dx, int dy);

    /** *//**
     * 清除像素
     * 
     * @param rgb
     */
    abstract void clear(int rgb);

    /** *//**
     * 获得LGraphics
     * 
     * @return
     */
    abstract LGraphics getLGraphics();

    abstract Image getImage();

    /** *//**
     * 设定指定x,y交集
     * 
     * @param x
     * @param y
     * @return
     */
    abstract boolean setTransparent(int x, int y);

    /** *//**
     * 设定指定color出现处
     * 
     * @param val
     */
    abstract void setTransparent(LColor color);

    /** *//**
     * 替换指定颜色
     * 
     * @param color1
     * @param color2
     */
    abstract void setDisplace(LColor color1, LColor color2);

    /** *//**
     * 保存自身到指定路径
     * 
     * @param path
     */
    abstract void save(String path);

    /** *//**
     * 保存图像到指定路径
     * 
     * @param image
     * @param path
     */
    abstract void save(Image image, String path);

    /** *//**
     * 保存图像到指定路径为指定大小
     * 
     * @param image
     * @param path
     * @param width
     * @param height
     */
    abstract void save(Image image, String path, int width, int height);

}


LColor.java:
package org.loon.framework.game.test.image;
/** *//**
 * <p>Title: LoonFramework</p>
 * <p>Description:loonframework用color</p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: LoonFramework</p>
 * @author chenpeng  
 * @email:ceponline@yahoo.com.cn 
 * @version 0.1
 */
public class LColor ...{
    
    
    int R=0;
    int G=0;
    int B=0;
    
    public LColor()...{}
    
    /** *//**
     * 注入r,g,b数值
     * @param r
     * @param g
     * @param b
     */
    public LColor(int r,int g,int b)...{
        this.R=r;
        this.G=g;
        this.B=b;
    }
}

LGraphics:
package org.loon.framework.game.test.image;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.font.LineMetrics;

import org.loon.framework.game.test.image.BaseImage;

/** *//**
 * <p>Title: LoonFramework</p>
 * <p>Description:loonframework用graphics,开发中……</p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: LoonFramework</p>
 * @author chenpeng  
 * @email:ceponline@yahoo.com.cn 
 * @version 0.1
 */
public class LGraphics ...{

    Graphics _g = null;

    Font _font;

    public LGraphics() ...{
        _font = new Font(null, Font.PLAIN, 12);
    }

    public LGraphics(Graphics g) ...{

        _font = new Font(null, Font.PLAIN, 12);

        setGraphics(g);

    }

    public void setGraphics(Graphics g) ...{

        _g = g;

        _g.setFont(_font);

    }

    public void setRGB(int rgb) ...{

        _g.setColor(new Color(rgb));

    }

    public void setRGB(int r, int g, int b) ...{

        int rgb = (r << 16) | (g << 8) | b;

        _g.setColor(new Color(rgb));

    }

    public void drawLine(int x1, int y1, int x2, int y2, int rgb) ...{

        setRGB(rgb);

        _g.drawLine(x1, y1, x2, y2);

    }

    public void drawRect(int x, int y, int width, int height) ...{
        _g.drawRect(x, y, width, height);
    }

    public void drawRect(int x, int y, int width, int height, int rgb) ...{

        setRGB(rgb);

        _g.drawRect(x, y, width, height);

    }

    public void fillRect(int x, int y, int width, int height) ...{
        _g.fillRect(x, y, width, height);
    }

    public void fillRect(int x, int y, int width, int height, int rgb) ...{

        setRGB(rgb);

        _g.fillRect(x, y, width, height);

    }

    public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) ...{

        if (_g == null) ...{
            return;
        }

        Polygon p = new Polygon();

        p.addPoint(x1, y1);
        p.addPoint(x2, y2);
        p.addPoint(x3, y3);

        _g.fillPolygon(p);

    }

    public void drawImage(BaseImage img, int x, int y) ...{
        _g.drawImage(img.getImage(), x, y, null);
    }

    public int getStringWidth(String str) ...{

        if (str == null || str.length() < 1) ...{
            return 0;
        }

        FontMetrics fm = _g.getFontMetrics();

        int w = 0;

        w = fm.stringWidth(str);

        return w;

    }

    public int getStringHeight(String str) ...{

        if (str == null || str.length() < 1) ...{
            return 0;
        }

        FontMetrics fm = _g.getFontMetrics();

        LineMetrics lm = fm.getLineMetrics(str, _g);

        int h = 0;

        h = (int)(lm.getHeight());

        return h;

    }

    public void drawString(String str, int x, int y) ...{

        y += getStringHeight(str);

        _g.drawString(str, x, y);

    }

    public void drawString(String str, int x, int y, int rgb) ...{

        setRGB(rgb);

        drawString(str, x, y);

    }

    public Graphics getGraphics() ...{
        return _g;
    }

}

Bitmap.java:(你没看错,偶就是在学M$……)
package org.loon.framework.game.test.image;

import java.awt.AlphaComposite;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.PixelGrabber;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/** *//**
 * <p>
 * Title: LoonFramework
 * </p>
 * <p>
 * Description:loonframework用Bitmap,封装了image的相关操作,开发中……
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: LoonFramework
 * </p>
 * 
 * @author chenpeng
 * @email:ceponline@yahoo.com.cn
 * @version 0.1
 */
public class Bitmap implements Cloneable, BaseImage ...{

    byte _bitmapFileHeader[] = new byte[14];

    byte _bfType[] = ...{ 'B', 'M' };

    int _bfReserved1 = 0;

    int _bfReserved2 = 0;

    int _bfOffBits = BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;

    byte _bitmapInfoHeader[] = new byte[40];

    int _biPlanes = 1;

    int _biBitCount = 24;

    int _biCompression = 0;

    int _biXPelsPerMeter = 0x0;

    int _biYPelsPerMeter = 0x0;

    int _biClrUsed = 0;

    int _biClrImportant = 0;

    int _bfSize = 0;

    int _biSizeImage = 0x030000;

    int _biSize = BITMAPINFOHEADER_SIZE;

    int _biWidth = 0;

    int _biHeight = 0;

    int _bitmap[];

    FileOutputStream _output;

    private BufferedImage _img;

    private LGraphics _g;

    private BufferedImage _back;

    private Graphics2D _bg;

    /** *//**
     * 将byte[]转为image
     * 
     * @param bytes
     * @return
     */
    public Image getImage(byte[] bytes) ...{
        return Toolkit.getDefaultToolkit().createImage(bytes);
    }

    /** *//**
     * 获得BufferedImage
     * 
     * @param width
     * @param height
     * @param data
     * @return
     */
    public BufferedImage getBufferedImage(int width, int height, byte[] data) ...{
        DataBuffer buffer = new DataBufferByte(data, width * height);
        int pixelStride = 4;
        int scanlineStride = 4 * width;
        int[] bandOffsets = ...{ 0, 1, 2 };
        WritableRaster raster = Raster.createInterleavedRaster(buffer, width,
                height, scanlineStride, pixelStride, bandOffsets, null);
        ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        boolean hasAlpha = false;
        boolean isAlphaPremultiplied = false;
        int transparency = Transparency.OPAQUE;
        int transferType = DataBuffer.TYPE_BYTE;
        ColorModel colorModel = new ComponentColorModel(colorSpace, hasAlpha,
                isAlphaPremultiplied, transparency, transferType);
        return new BufferedImage(colorModel, raster, isAlphaPremultiplied, null);
    }

    /** *//**
     * 生成指定宽与高的空白图像。
     * 
     * @param width
     * @param height
     */
    public Bitmap(int width, int height) ...{

        try ...{
            _img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        } catch (Exception e) ...{
            _img = null;
        }

        if (_img != null) ...{

            _g = new LGraphics();
            _g.setGraphics(_img.createGraphics());

        } else ...{
            _g = null;
        }

        try ...{
            _back = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        } catch (Exception e) ...{
            _back = null;
        }

        if (_back != null) ...{
            _bg = _back.createGraphics();
        } else ...{
            _bg = null;
        }

    }

    /** *//**
     * 追踪载入图像的实例
     * 
     * @param image
     */
    final private void trackerImage(Image image) ...{
        MediaTracker mediaTracker = new MediaTracker(new Container());
        mediaTracker.addImage(image, 0);
        try ...{
            mediaTracker.waitForID(0);
        } catch (InterruptedException e) ...{
            e.printStackTrace();
        }

    }

    /** *//**
     * 以pixels构造bitmap
     * 
     * @param width
     * @param height
     * @param pixels
     */
    public Bitmap(int width, int height, int[] pixels) ...{

        try ...{
            _img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        } catch (Exception e) ...{
            _img = null;
        }

        if (_img != null) ...{

            _g = new LGraphics();

            _g.setGraphics(_img.getGraphics());

        } else ...{
            _g = null;
        }

        if (_img == null || pixels.length < width * height) ...{
            return;
        }

        for (int i = 0; i < height; i++) ...{
            for (int j = 0; j < width; j++) ...{
                setPixel(j, i, pixels[j + i * width]);
            }
        }

        try ...{
            _back = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        } catch (Exception e) ...{
            _back = null;
        }

        if (_back != null) ...{
            _bg = _back.createGraphics();
        } else ...{
            _bg = null;
        }

    }

    /** *//**
     * 以指定路径载入bitmap
     * 
     * @param path
     */
    public Bitmap(String path) ...{
        this(getDataSource(path));
    }

    /** *//**
     * 以inputstream载入bitmap
     * 
     * @param inputstream
     */
    public Bitmap(InputStream inputstream) ...{
        this(getDataSource(inputstream));
    }

    /** *//**
     * 将byte[]转为bitmap
     * 
     * @param data
     */
    public Bitmap(byte[] data) ...{
        this(Toolkit.getDefaultToolkit().createImage(data));
    }

    /** *//**
     * 将image直接注入bitmap
     * 
     * @param data
     */
    public Bitmap(Image image) ...{
        try ...{
            _img = getBufferedImage(image);
        } catch (Exception e) ...{
            _img = null;
        }
        if (_img != null) ...{
            _g = new LGraphics();
            _g.setGraphics(_img.createGraphics());

            try ...{
                _back = new BufferedImage(_img.getWidth(), _img.getHeight(),
                        BufferedImage.TYPE_INT_RGB);
            } catch (Exception e) ...{
                _back = null;
            }
            if (_back != null) ...{
                _bg = _back.createGraphics();
            } else ...{
                _bg = null;
            }

        } else ...{
            _g = null;
        }

    }

    /** *//**
     * 通过物理路径获得byte[]
     * 
     * @param name
     * @return
     */
    final static private byte[] getDataSource(String name) ...{
        FileInputStream fileInput;
        try ...{
            fileInput = new FileInputStream(new File(name));
        } catch (FileNotFoundException e) ...{
            fileInput = null;
        }
        BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
        return getDataSource(bufferedInput);
    }

    /** *//**
     * 将image转为BufferedImage
     * 
     * @param image
     * @return
     */
    public BufferedImage getBufferedImage(Image image) ...{
        trackerImage(image);
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        BufferedImage bufferedimage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedimage.getGraphics();
        g.drawImage(image, 0, 0, null);
        return bufferedimage;
    }

    /** *//**
     * 通过InputStream获得byte[]
     * 
     * @param is
     * @return
     */
    final static private byte[] getDataSource(InputStream is) ...{
        if (is == null) ...{
            return null;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] arrayByte = null;
        try ...{
            byte[] bytes = new byte[4096];
            bytes = new byte[is.available()];
            int read;
            while ((read = is.read(bytes)) >= 0) ...{
                byteArrayOutputStream.write(bytes, 0, read);
            }
            arrayByte = byteArrayOutputStream.toByteArray();
        } catch (IOException e) ...{
            return null;
        } finally ...{
            try ...{
                if (byteArrayOutputStream != null) ...{
                    byteArrayOutputStream.close();
                    byteArrayOutputStream = null;
                }
                if (is != null) ...{
                    is.close();
                    is = null;
                }

            } catch (IOException e) ...{
            }
        }
        // 返回byte[]
        return arrayByte;
    }

    /** *//**
     * 获得图像宽
     */
    public int getWidth() ...{

        if (_img == null) ...{
            return 0;
        }

        return _img.getWidth();

    }

    /** *//**
     * 获得图像高
     */
    public int getHeight() ...{

        if (_img == null) ...{
            return 0;
        }

        return _img.getHeight();

    }

    /** *//**
     * 获得指定x,y交集处的图像color
     */
    public LColor getPixel(int x, int y) ...{
        if (_img == null || x < 0 || x >= getWidth() || y < 0
                || y >= getHeight()) ...{
            return null;
        }
        int c = _img.getRGB(x, y);
        LColor color = new LColor();
        color.R = (c & 0x00ff0000) >> 16;
        color.G = (c & 0x0000ff00) >> 8;
        color.B = c & 0x000000ff;
        return color;
    }

    /** *//**
     * 返回image实例
     */
    public Image getImage() ...{
        return _img;
    }

    /** *//**
     * 返回Graphics
     */
    public LGraphics getLGraphics() ...{
        return _g;
    }

    /** *//**
     * 设定指定x,y交集处rgb颜色
     */
    public boolean setPixel(int x, int y, int rgb) ...{

        if (_img == null || x < 0 || x >= getWidth() || y < 0
                || y >= getHeight()) ...{
            return false;
        }

        _img.setRGB(x, y, rgb);

        return true;

    }

    /** *//**
     * 设定指定x,y交集处rgb颜色(分别注入)
     */
    public boolean setPixel(int x, int y, int r, int g, int b) ...{

        if (_img == null || x < 0 || x >= getWidth() || y < 0
                || y >= getHeight()) ...{
            return false;
        }

        int rgb = (r << 16) | (g << 8) | b;

        _img.setRGB(x, y, rgb);

        return true;

    }

    /** *//**
     * 设定指定x,y交集处color
     * 
     * @param x
     * @param y
     * @param color
     * @return
     */
    public boolean setPixel(int x, int y, LColor color) ...{
        if (_img == null || x < 0 || x >= getWidth() || y < 0
                || y >= getHeight()) ...{
            return false;
        }
        int rgb = (color.R << 16) | (color.G << 8) | color.B;
        _img.setRGB(x, y, rgb);
        return true;
    }

    /** *//**
     * 设定指定x,y交集像素点。
     */
    public boolean setTransparent(int x, int y) ...{
        if (_img == null || x < 0 || x >= getWidth() || y < 0
                || y >= getHeight()) ...{
            return false;
        }
        _img.setRGB(x, y, 0x00000000);
        return true;

    }

    /** *//**
     * 设定整个Bitmap的透明度
     * 
     * @param d
     */
    public void setScaleTransform(double d) ...{
        int width = getWidth();
        int height = getHeight();
        BufferedImage back = null;
        Graphics bottom = null;
        Image screen = null;
        Graphics gb = null;
        try ...{
            back = new BufferedImage(width, height, 2);
            bottom = back.getGraphics();
            screen = new BufferedImage(width, height, 1);
            gb = screen.getGraphics();
            AlphaComposite alphacomposite = AlphaComposite.getInstance(3,
                    (float) d);
            ((Graphics2D) gb).setComposite(alphacomposite);
            gb.drawImage(_img, 0, 0, null);
            bottom.drawImage(screen, 0, 0, null);
            _img = back;
        } catch (Exception ex) ...{

        } finally ...{
            if (gb != null) ...{
                gb = null;
            }
            if (screen != null) ...{
                screen.flush();
                screen = null;
            }
            if (bottom != null) ...{
                bottom = null;
            }
            if (back != null) ...{
                back.flush();
                back = null;
            }
        }

    }

    /** *//**
     * 设定image中指定color数值
     */
    public void setTransparent(LColor color) ...{

        WritableRaster wr = _img.getRaster();
        int width = getWidth();
        int height = getHeight();
        if (wr == null || width < 1 || height < 1) ...{
            return;
        }
        for (int i = 0; i < width; i++) ...{
            for (int j = 0; j < height; j++) ...{
                int c = _img.getRGB(i, j);
                LColor cr = new LColor();
                cr.R = (c & 0x00ff0000) >> 16;
                cr.G = (c & 0x0000ff00) >> 8;
                cr.B = c & 0x000000ff;
                if ((cr.R == color.R) && (cr.G == color.G) && (cr.B == color.B)) ...{
                    wr.setSample(i, j, 0, 0x00000000);
                }
            }
        }
    }

    /** *//**
     * 替换指定颜色
     * 
     * @param color1
     * @param color2
     */
    public void setDisplace(LColor color1, LColor color2) ...{
        WritableRaster wr = _img.getRaster();
        int width = getWidth();
        int height = getHeight();
        if (wr == null || width < 1 || height < 1) ...{
            return;
        }
        for (int x = 0; x < width; x++) ...{
            for (int y = 0; y < height; y++) ...{
                int c = _img.getRGB(x, y);
                LColor cr = new LColor();
                cr.R = (c & 0x00ff0000) >> 16;
                cr.G = (c & 0x0000ff00) >> 8;
                cr.B = c & 0x000000ff;
                if ((cr.R == color1.R) && (cr.G == color1.G)
                        && (cr.B == color1.B)) ...{
                    int rgb = (color2.R << 16) | (color2.G << 8) | color2.B;
                    _img.setRGB(x, y, rgb);
                }
            }
        }
    }

    /** *//**
     * 移动指定像素点
     */
    public void movePixels(int dx, int dy) ...{

        _bg.drawImage(_img, null, 0, 0);

        ((Graphics2D) (_g.getGraphics())).drawImage(_back, null, dx, dy);

    }

    /** *//**
     * 克隆自身
     */
    final public Object clone() throws CloneNotSupportedException ...{
        return (Bitmap) super.clone();
    }

    /** *//**
     * bmp文件保存处理,内部调用。
     * 
     * @return
     */
    final private BMPSave BMP() ...{
        return new BMPSave();
    }

    /** *//**
     * 保存自身到指定路径
     * 
     * @param path
     */
    public void save(String path) ...{
        this.BMP().saveBitmap(path, _img, _img.getWidth(null),
                _img.getHeight(null));
    }

    /** *//**
     * 保存图像到指定路径
     * 
     * @param image
     * @param path
     */
    public void save(Image image, String path) ...{
        this.BMP().saveBitmap(path, image, image.getWidth(null),
                image.getHeight(null));
    }

    /** *//**
     * 保存图像到指定路径为指定大小
     * 
     * @param image
     * @param path
     * @param width
     * @param height
     */
    public void save(Image image, String path, int width, int height) ...{
        this.BMP().saveBitmap(path, image, width, height);
    }

    /** *//**
     * 清除指定rgb
     */
    public void clear(int rgb) ...{

        if (_img == null || _g == null) ...{
            return;
        }

        _g.setRGB(rgb);

        _g.fillRect(0, 0, _img.getWidth(), _img.getHeight());

    }

    class BMPSave ...{

        /** *//**
         * 
         */
        private static final long serialVersionUID = 1L;

        /** *//**
         * 仅限此类内部使用
         * 
         */
        private BMPSave() ...{
        }

        public void saveBitmap(String parFilename, Image parImage,
                int parWidth, int parHeight) ...{
            try ...{
                _output = new FileOutputStream(parFilename);
                save(parImage, parWidth, parHeight);
                _output.close();
            } catch (Exception saveEx) ...{
                saveEx.printStackTrace();
            }
        }

        private void save(Image parImage, int parWidth, int parHeight) ...{
            try ...{
                convertImage(parImage, parWidth, parHeight);
                writeBitmapFileHeader();
                writeBitmapInfoHeader();
                writeBitmap();
            } catch (Exception saveEx) ...{
                saveEx.printStackTrace();
            }
        }

        private boolean convertImage(Image parImage, int parWidth, int parHeight) ...{
            int pad;
            _bitmap = new int[parWidth * parHeight];
            PixelGrabber pg = new PixelGrabber(parImage, 0, 0, parWidth,
                    parHeight, _bitmap, 0, parWidth);
            try ...{
                pg.grabPixels();
            } catch (InterruptedException e) ...{
                e.printStackTrace();
                return (false);
            }
            pad = (4 - ((parWidth * 3) % 4)) * parHeight;
            _biSizeImage = ((parWidth * parHeight) * 3) + pad;
            _bfSize = _biSizeImage + BITMAPFILEHEADER_SIZE
                    + BITMAPINFOHEADER_SIZE;
            _biWidth = parWidth;
            _biHeight = parHeight;
            return (true);
        }

        private void writeBitmap() ...{
            int size;
            int value;
            int j;
            int i;
            int rowCount;
            int rowIndex;
            int lastRowIndex;
            int pad;
            int padCount;
            byte rgb[] = new byte[3];
            size = (_biWidth * _biHeight) - 1;
            pad = 4 - ((_biWidth * 3) % 4);
            if (pad == 4) ...{
                pad = 0;
            }
            rowCount = 1;
            padCount = 0;
            rowIndex = size - _biWidth;
            lastRowIndex = rowIndex;
            try ...{
                for (j = 0; j < size; j++) ...{
                    value = _bitmap[rowIndex];
                    rgb[0] = (byte) (value & 0xFF);
                    rgb[1] = (byte) ((value >> 8) & 0xFF);
                    rgb[2] = (byte) ((value >> 16) & 0xFF);
                    _output.write(rgb);
                    if (rowCount == _biWidth) ...{
                        padCount += pad;
                        for (i = 1; i <= pad; i++) ...{
                            _output.write(0x00);
                        }
                        rowCount = 1;
                        rowIndex = lastRowIndex - _biWidth;
                        lastRowIndex = rowIndex;
                    } else
                        rowCount++;
                    rowIndex++;
                }
                _bfSize += padCount - pad;
                _biSizeImage += padCount - pad;
            } catch (Exception wb) ...{
                wb.printStackTrace();
            }
        }

        private void writeBitmapFileHeader() ...{
            try ...{
                _output.write(_bfType);
                _output.write(intToDWord(_bfSize));
                _output.write(intToWord(_bfReserved1));
                _output.write(intToWord(_bfReserved2));
                _output.write(intToDWord(_bfOffBits));
            } catch (Exception wbfh) ...{
                wbfh.printStackTrace();
            }
        }

        private void writeBitmapInfoHeader() ...{
            try ...{
                _output.write(intToDWord(_biSize));
                _output.write(intToDWord(_biWidth));
                _output.write(intToDWord(_biHeight));
                _output.write(intToWord(_biPlanes));
                _output.write(intToWord(_biBitCount));
                _output.write(intToDWord(_biCompression));
                _output.write(intToDWord(_biSizeImage));
                _output.write(intToDWord(_biXPelsPerMeter));
                _output.write(intToDWord(_biYPelsPerMeter));
                _output.write(intToDWord(_biClrUsed));
                _output.write(intToDWord(_biClrImportant));
            } catch (Exception wbih) ...{
                wbih.printStackTrace();
            }
        }

        private byte[] intToWord(int parValue) ...{
            byte retValue[] = new byte[2];
            retValue[0] = (byte) (parValue & 0x00FF);
            retValue[1] = (byte) ((parValue >> 8) & 0x00FF);
            return (retValue);
        }

        private byte[] intToDWord(int parValue) ...{
            byte retValue[] = new byte[4];
            retValue[0] = (byte) (parValue & 0x00FF);
            retValue[1] = (byte) ((parValue >> 8) & 0x000000FF);
            retValue[2] = (byte) ((parValue >> 16) & 0x000000FF);
            retValue[3] = (byte) ((parValue >> 24) & 0x000000FF);
            return (retValue);
        }

    }

}





使用示例,实现了一个图形的油画效果:




package org.loon.framework.game.test.image;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.util.Random;

/** *//**
 * <p>
 * Title: LoonFramework
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: LoonFramework
 * </p>
 * 
 * @author chenpeng
 * @email:ceponline@yahoo.com.cn
 * @version 0.1
 */

public class MyPanel extends Panel ...{

    /** *//**
     * 
     */
    private static final long serialVersionUID = 1L;

    Bitmap _bitmap = null;

    public MyPanel(Bitmap bitmap) ...{

        _bitmap = bitmap;

        setLayout(null);

        setBackground=\'#\'" />    }

    public void paint(Graphics g) ...{

        // 加载原始图
        g.drawImage(_bitmap.getImage(), 0, 0, this);
        // 图像间隔
        int space = 2;
        // 加载loonframework color
        LColor color = new LColor();
        // 产生随机数
        Random rand = new Random();
        // 偏移度
        int offset = 3;
        for (int i = 0; i < _bitmap.getWidth() - offset; i++) ...{
            for (int j = 0; j < _bitmap.getHeight() - offset; j++) ...{
                // 产生随机颗粒
                int a = rand.nextInt(1000) % offset;
                color = _bitmap.getPixel(i + a, j + a);
                // 重新注入r,g,b
                _bitmap.setPixel(i, j, color);
            }
        }
        // 绘制
        g.drawImage(_bitmap.getImage(), _bitmap.getWidth()+space, 0, this);

    }

    public static void main(String[] args) ...{

        Frame frm = new Frame("Java油画效果(本示例由Loonframework框架提供)");
        frm.setSize(600, 364);
        frm.setResizable(false);
        frm.add(new MyPanel(new Bitmap("c:/cc0.jpg")));
        frm.setVisible(true);

    }

}



 
原图与示例效果图 



顶 2踩 0收藏
文章评论
共1 条评论 1/1页
发表评论

个人资料

  • 昵称: 海大软件1102班
  • 等级: 初级设计师
  • 积分: 2516
  • 代码: 147 个
  • 文章: 70 篇
  • 随想: 11 条
  • 访问: 242 次
  • 关注

站长推荐