使用Jhlabs图像处理库的圆柱形状图像

时间:2016-03-30 08:54:31

标签: java android graphic

我想在我的项目中实现图像处理。我曾经使用过JLabs库。我想在效果之后使用这个库。

1)Deboss 2)雕刻 3)缎纹蚀刻 4)自定义形状如Cylinder。

这是样本图像浮雕代码。

public class EmbossFilter
        extends WholeImageFilter {
    private static final float pixelScale = 255.9f;
    private float azimuth = 2.3561945f;
    private float elevation = 0.5235988f;
    private boolean emboss = false;
    private float width45 = 3.0f;

    public void setAzimuth(float azimuth) {
        this.azimuth = azimuth;
    }

    public float getAzimuth() {
        return this.azimuth;
    }

    public void setElevation(float elevation) {
        this.elevation = elevation;
    }

    public float getElevation() {
        return this.elevation;
    }

    public void setBumpHeight(float bumpHeight) {
        this.width45 = 3.0f * bumpHeight;
    }

    public float getBumpHeight() {
        return this.width45 / 3.0f;
    }

    public void setEmboss(boolean emboss) {
        this.emboss = emboss;
    }

    public boolean getEmboss() {
        return this.emboss;
    }

    @Override
    protected int[] filterPixels(int width, int height, int[] inPixels, Rect transformedSpace) {
        int index = 0;
        int[] outPixels = new int[width * height];
        int bumpMapWidth = width;
        int bumpMapHeight = height;
        int[] bumpPixels = new int[bumpMapWidth * bumpMapHeight];
        int i = 0;
        while (i < inPixels.length) {
            bumpPixels[i] = PixelUtils.brightness(inPixels[i]);
            ++i;
        }
        int Lx = (int) (Math.cos(this.azimuth) * Math.cos(this.elevation) * 255.89999389648438);
        int Ly = (int) (Math.sin(this.azimuth) * Math.cos(this.elevation) * 255.89999389648438);
        int Lz = (int) (Math.sin(this.elevation) * 255.89999389648438);
        int Nz = (int) (1530.0f / this.width45);
        int Nz2 = Nz * Nz;
        int NzLz = Nz * Lz;
        int background = Lz;
        int bumpIndex = 0;
        int y = 0;
        while (y < height) {
            int s1 = bumpIndex;
            int s2 = s1 + bumpMapWidth;
            int s3 = s2 + bumpMapWidth;
            int x = 0;
            while (x < width) {
                int shade;
                if (y != 0 && y < height - 2 && x != 0 && x < width - 2) {
                    int NdotL;
                    int Nx = bumpPixels[s1 - 1] + bumpPixels[s2 - 1] + bumpPixels[s3 - 1] - bumpPixels[s1 + 1] - bumpPixels[s2 + 1] - bumpPixels[s3 + 1];
                    int Ny = bumpPixels[s3 - 1] + bumpPixels[s3] + bumpPixels[s3 + 1] - bumpPixels[s1 - 1] - bumpPixels[s1] - bumpPixels[s1 + 1];
                    shade = Nx == 0 && Ny == 0 ? background : ((NdotL = Nx * Lx + Ny * Ly + NzLz) < 0 ? 0 : (int) ((double) NdotL / Math.sqrt(Nx * Nx + Ny * Ny + Nz2)));
                } else {
                    shade = background;
                }
                if (this.emboss) {
                    int rgb = inPixels[index];
                    int a = rgb & -16777216;
                    int r = rgb >> 16 & 255;
                    int g = rgb >> 8 & 255;
                    int b = rgb & 255;
                    r = r * shade >> 8;
                    g = g * shade >> 8;
                    b = b * shade >> 8;
                    outPixels[index++] = a | r << 16 | g << 8 | b;
                } else {
                    outPixels[index++] = -16777216 | shade << 16 | shade << 8 | shade;
                }
                ++x;
                ++s1;
                ++s2;
                ++s3;
            }
            ++y;
            bumpIndex += bumpMapWidth;
        }
        return outPixels;
    }

    public String toString() {
        return "Stylize/Emboss...";
    }
}

如何使用Jhlabs库或任何其他方式实现图像效果请与我分享。

0 个答案:

没有答案