I looked everywhere, but there seemed to be no standard (I could see) how one could check if the image was blank. In c #
I have a way to do this, but I would really like to know what is the right way to check if the image is empty, so everyone can know in the future.
I'm not going to copy paste a bunch of code, if you want it, I will be pleased, but I just want to explain how I can check if the image is empty.
You take a .jpg image, get its width. For example, 500 pixels Then you divide it by 2 giving you 250
Then you check what color of each pixel is in place (width 250 and height i) (where you repeat the thought of image height.
What this means is only checking the center line of the image pixels vertically. It looks like all the pixels are checking to see if there is any color other than white. I did this, so you don’t have to search ALL 500 * pixel heights, since you almost always encounter color in the middle of the page.
His work ... a little slow ... Should there be a better way to do this? You can change it to search 2/3/4 lines vertically to increase the likelihood that the page will not be blank, but it will take even longer.
(Also note: using the image size to verify that it contains something will not work in this case, since the page with two sentences and the empty page size are too close to each other)
After adding the solution.
Resources that will help in the implementation and understanding of the solution.
(Note that on the first website, the claimed Pizelformat is actually Pixelformat). The little mistake that I know, just mentioning, can cause some confusion for some.
After I implemented the method of accelerating the search for pixels, the speed did not increase. Therefore, I think that I am doing something wrong.
Old time = 15.63 for 40 images.
New time = 15.43 for 40 images
I saw with a large DocMax quoted article that the code is "blocked" in a set of pixels. (or, as I understand it) So, I did a lock in the middle row of pixels of each page. Will this be the right step?
private int testPixels(String sourceDir) { //iterate through images string[] fileEntries = Directory.GetFiles(sourceDir).Where(x => x.Contains("JPG")).ToArray(); var q = from string x in Directory.GetFiles(sourceDir) where x.ToLower().EndsWith(".jpg") select new FileInfo(x); int holder = 1; foreach (var z in q) { Bitmap mybm= Bitmap.FromFile(z.FullName) as Bitmap; int blank = getPixelData2(mybm); if (blank == 0) { holder = 0; break; } } return holder; }
And then the class
private unsafe int getPixelData2(Bitmap bm) { BitmapData bmd = bm.LockBits(new System.Drawing.Rectangle((bm.Width / 2), 0, 1, bm.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bm.PixelFormat); int blue; int green; int red; int width = bmd.Width / 2; for (int y = 0; y < bmd.Height; y++) { byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride); blue = row[width * 3]; green = row[width * 2]; red = row[width * 1];