Home>

I'm trying to reprint what I developed on Xmarin.Android into a Swift version.

Swift itself is my first time studying and I'm stuck in various ways.
Among them, I don't understand how to handle images.

Basically, get the pixel at the X and Y coordinates of the image, get the color information,
I am doing each image processing, but first of all, when processing such as binarization
I would like to know how to get pixels with X, Y.

The process written in C # is described below.
The processing contents are as follows.


(1) Obtain only the range of the frame specified by the argument in pixel units based on the bitmap image of the argument.
(2) Judge the hue of the pixel, binarize it, and update the image of the return value.

What I don't know in Swift is how to get pixel information from UIImage and process it.
In particular, there seem to be multiple methods depending on the version of Swift, which is also one of the causes of confusion.

If anyone knows about it, we apologize for the inconvenience, but thanks for your professor.

Public static Bitmap Monocrome (Bitmap bitmap, Rect rect)
        {
            Bitmap outBitMap = bitmap.Copy (Bitmap.Config.Argb8888, true);
            int x, y;
            for (x = rect.Left;x<= rect.Right;x ++)
            {
                for (y = rect.Top;y<= rect.Bottom;y ++)
                {
                    int pixelColor = outBitMap.GetPixel (x, y);
                    var c = new Color (pixelColor);
                    if (166<= c.GetHue ()&&c.GetHue ()<= 259)
                    {
                        c = Color.White;
                    }
                    else else
                    {
                        c = Color.Black;
                    }
                    outBitMap.SetPixel (x, y, c);
                }
            }
            return outBitMap;
        }
  • Answer # 1

    Was self resolved.
    It's not a very clean source, but I was able to achieve the same thing below.

      /// Binarization (within the specified range)
        /// --Parameters:
        /// --image: Binarized image
        /// --rect: Binarized area
        ///-Returns: Binarized image
        public func Monocrome (image: UIImage, rect: CGRect)->UIImage
        {
            guard let imageData = image.cgImage? .DataProvider? .Data else {return image}
            let data: UnsafePointer<UInt8>= CFDataGetBytePtr (imageData)
            let iWidth = Int (image.size.width)
            // let iHeight = Int (image.size.height)
            var hue: CGFloat = 0
            var saturation: CGFloat = 0
            var brightness: CGFloat = 0
            var alpha: CGFloat = 0
            let renderer = UIGraphicsImageRenderer (size: image.size)
            guard let cgImage = renderer.image (actions: {renderenderContext in
                for x in ((Int) (rect.minX) ... (Int) (rect.maxX))
                {
                    for y in ((Int) (rect.minY) ... (Int) (rect.maxY))
                    {
                        let point = CGPoint (x: x, y: y)
                        let pixelInfo: Int = ((iWidth * Int (point.y)) + Int (point.x)) * 4
                        let red = CGFloat (data [pixelInfo])
                        let green = CGFloat (data [pixelInfo + 1])
                        let blue = CGFloat (data [pixelInfo + 2])
                        alpha = CGFloat (data [pixelInfo + 3])
                        let color = UIColor (red: red/255.0,
                                            green: green/255.0,
                                            blue: blue/255.0,
                                            alpha: alpha/255.0)
                        color.getHue (&hue, saturation:&saturation, brightness:&brightness, alpha:&alpha)
                        if (166/360<= hue&&hue<= 259/360)
                        {
                            //White
                            rendererContext.cgContext.setFillColor (UIColor.white.cgColor)
                            rendererContext.fill (CGRect (x: x, y: y, width: 1, height: 1))
                        }
                        else else
                        {
                            //black
                            rendererContext.cgContext.setFillColor (UIColor.black.cgColor)
                            rendererContext.fill (CGRect (x: x, y: y, width: 1, height: 1))
                        }
                    }
                }
            }). CgImage else {return image}
            return UIImage (cgImage: cgImage)
        }