package com.KIO4_CompareImages;
// Juan Antonio Villalpando - kio4.com
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.UsesPermissions; // Permisos
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.*;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@DesignerComponent(version = 1,
description = "Compare pixel to pixel two images. Juan Antonio Villalpando - KIO4.COM ",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.READ_EXTERNAL_STORAGE")
public class KIO4_CompareImages extends AndroidNonvisibleComponent implements Component {
public static final int VERSION = 1;
private ComponentContainer container;
private double percentage = 0.0;
private Bitmap bmp1;
private Bitmap bmp2;
public KIO4_CompareImages(ComponentContainer container) {
super(container.$form());
this.container = container;
}
////////////////////// FUNCIONES //////////////////////////////////////////////////////////////////////
@SimpleFunction(description = "pathImage, absolute address, example: /mnt/sdcard/image.png or start with file:// example file:///mnt/sdcard/image.png. Get a decimal number 0 to 100.")
public double CompareImages(String pathImage1, String pathImage2){
pathImage1 = pathImage1.replaceFirst("file://", "");
bmp1 = BitmapFactory.decodeFile(pathImage1);
bmp1 = bmp1.copy(Bitmap.Config.ARGB_8888 , true);
pathImage2 = pathImage2.replaceFirst("file://", "");
bmp2 = BitmapFactory.decodeFile(pathImage2);
bmp2 = bmp2.copy(Bitmap.Config.ARGB_8888 , true);
// Assigning dimensions to image
int width1 = bmp1.getWidth();
int height1 = bmp1.getHeight();
int width2 = bmp2.getWidth();
int height2 = bmp2.getHeight();
// Checking whether the images are of same size or not
if ((width1 != width2) || (height1 != height2))
percentage = 0.0;
else {
// By now, images are of same size
long difference = 0;
// treating images likely 2D matrix
for (int y = 0; y < height1; y = y + 2) { // original y++
for (int x = 0; x < width1; x = x + 2) { // original x++
int rgbA = bmp1.getPixel(x, y);
int rgbB = bmp2.getPixel(x, y);
int redA = (rgbA >> 16) & 0xff;
int greenA = (rgbA >> 8) & 0xff;
int blueA = (rgbA)&0xff;
int redB = (rgbB >> 16) & 0xff;
int greenB = (rgbB >> 8) & 0xff;
int blueB = (rgbB)&0xff;
difference += Math.abs(redA - redB);
difference += Math.abs(greenA - greenB);
difference += Math.abs(blueA - blueB);
}
}
// Total number of red pixels = width * height
// Total number of blue pixels = width * height
// Total number of green pixels = width * height
// So total number of pixels = width * height *
// 3
double total_pixels = width1 * height1 * 3;
// Normalizing the value of different pixels for accuracy
// Note: Average pixels per color component
double avg_different_pixels = difference / total_pixels;
// There are 255 values of pixels in total
percentage = (avg_different_pixels / 255) * 100;
}
return percentage;
}
} // ==> FIN
I have changed this line to get faster.
for (int y = 0; y < height1; y = y + 2) { // original y++
for (int x = 0; x < width1; x = x + 2) { // original x++
To inspect a PCB, you would have to have an identical image of the perfect PCB and take an identical photo of the damaged PCB, which is probably not possible. I think there would have to be AI to check PCBs.
Actually I want to make my burglar alarm. This project seems suitable for this. It will compare the continuous images. It will write the difference value to the screen. I will ring the alarm according to the value range. My first problem is can I do this image comparison instantly.
I want the camera to be on all the time and to sound an alarm when it detects motion. How can I do it? Thank you. I did such a study about the sound but I don't know how to do it about the image. I'm trying to learn, thank you for your understanding.