package com.poker.control.delegate;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.RGBImageFilter;
import java.awt.image.ReplicateScaleFilter;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.poker.data.PokerData;
import com.poker.ui.ocr.OCR;
public class OCRDelegate {
OCR ocr;
public OCRDelegate() {
this.ocr = new OCR();
}
public String[] getStacksStrings(final PokerData pdata, final BufferedImage[] images) {
final String[] strStacks = new String[images.length];
final String str2;
final String str;
for (int i = 0; i < images.length; i++) {
try {
ImageIO.write(images[i], "BMP", new File(pdata.getProgramSettings()
.getExchangeFolder()
+ "tmp.bmp"));
strStacks[i] = this.ocr.showString(
pdata.getProgramSettings().getExchangeFolder() + "tmp.bmp").trim();
} catch (final IOException e) {
e.printStackTrace();
}
}
return strStacks;
}
public BufferedImage[] getPreparedStacksImages(final PokerData pdata,
final BufferedImage[] images, final int scale) {
for (int i = 0; i < images.length; i++) {
images[i] = prepareBMP(images[i], scale);
}
try {
for (int j = 0; j < 10; j++) {
ImageIO.write(images[j], "BMP", new File("c:\\temp\\0_" + j + ".bmp"));
}
} catch (final IOException e) {
e.printStackTrace();
}
return images;
}
protected BufferedImage prepareBMP(final BufferedImage bi, final int scale) {
final int height = bi.getHeight();
final int width = bi.getWidth();
final BufferedImage imageOut = new BufferedImage(width * scale, (height * scale),
BufferedImage.TYPE_BYTE_GRAY);
final ImageFilter filter = new RGBImageFilter() {
@Override
public int filterRGB(final int x, final int y, final int rgb) {
final int rgb2 = rgb & 0xFF000000;
final int r = (rgb & 0x00FF0000) >> 16;
final int g = (rgb & 0x0000FF00) >> 8;
final int b = rgb & 0x000000FF;
if (255 - r < 20 && 255 - g < 20 && 255 - b < 20) {
return rgb2 + 0;
} else {
return 0xFFFFFFFF;
}
}
};
final ReplicateScaleFilter rsc = new ReplicateScaleFilter(width * scale, height * scale);
Image tmpImg;
tmpImg = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(new FilteredImageSource(bi.getSource(), filter), rsc));
imageOut.getGraphics().drawImage(tmpImg, 0, 0, null);
return imageOut;
}
}