English 中文(简体)
更改简略的 Java典
原标题:Converting a simple JavaScript code

我曾试图转换,该版的《 Java成典代码,该图画从图像中获取主要颜色,但迄今没有成功。 我对<代码>colorCount &color变量有错误。 我不知道这些变量所用的适当和对应数据类型。 我的守则是:

public string dominantColor(Bitmap img)
{
       int[] colorCount = new int[0];
       int maxCount = 0;
       string dominantColor = "";

       // data is an array of a series of 4 one-byte values representing the rgba values of each pixel
       Bitmap Bmp = new Bitmap(img);

       BitmapData BmpData = Bmp.LockBits(new Rectangle(0, 0, Bmp.Width, Bmp.Height), ImageLockMode.ReadOnly, Bmp.PixelFormat);
       byte[] data = new byte[BmpData.Stride * Bmp.Height];


       for (int i = 0; i < data.Length; i += 4) 
       {
            // ignore transparent pixels
            if (data[i+3] == 0)
                continue;

            string color = data[i] + "." + data[i+1] + "," + data[i+2];
            // ignore white
            if (color == "255,255,255")
               continue;

            if (colorCount[color] != 0)
               colorCount[color] = colorCount[color] + 1;
            else
               colorCount[color] = 0;

            // keep track of the color that appears the most times
            if (colorCount[color] > maxCount)
            {
                 maxCount = colorCount[color];
                 dominantColor = color.ToString;
            }
       }

       string rgb = dominantColor.Split(",");
       return rgb;   
}
最佳回答

请允许我向您介绍一下您的法典经过全面管理的文本:

static Color dominantColor(Bitmap img)
{
    Hashtable colorCount = new Hashtable();
    int maxCount = 0;
    Color dominantColor = Color.White;

    for (int i = 0; i < img.Width; i++)
    {
        for (int j = 0; j < img.Height; j++)
        {
            var color = img.GetPixel(i, j);

            if (color.A == 0)
                continue;

            // ignore white
            if (color.Equals(Color.White))
                continue;

            if (colorCount[color] != null)
                colorCount[color] = (int)colorCount[color] + 1;
            else
                colorCount.Add(color, 0);

            // keep track of the color that appears the most times
            if ((int)colorCount[color] > maxCount)
            {
                maxCount = (int)colorCount[color];
                dominantColor = color;
            }
        }
    }

    return dominantColor;
}

So what is the difference here? - I use a Hashtable instead of your array (you never redefine the dimension of it - and the best way to use an extensible object from JavaScript is a Hashtable) - I prefer to use the already included structure Color (which saves 4 bytes for Alpha, Red, Green, Blue) - I also do the comparisons and return this structure (then you are free to do whatever you want to do - in JavaScript using those strings is just a workaround because the browser is just giving you such RGB(a) strings)

贵国法典的另一个问题是含有以下数据线:由星号数据 = 新的星号(BmpData.Stride * Bmp.Hala); 创建和初步使用你的阵列,但无数据(NET将抹掉所有以前的数据,造成大量零)。 因此,你不会在任何地方。

我版本的缺点是,它实际上很小(这是你正在玩弄锁脚的地方)。 如果你想要的话,我可以给你一个没有管理的版本(使用锁定和不安全锁)。 取决于业绩是否对你有利,如果你感兴趣的话!

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签