use of java.awt.image.RescaleOp in project HearthStats.net-Uploader by HearthStats.
the class OcrBase method filter.
/**
* Filters the image to make it easier to OCR, such as by turning it greyscale and increasing the contrast.
*
* @param image A cropped image
* @param iteration The iteration number, zero-based
* @return
* @throws OcrException
*/
protected BufferedImage filter(BufferedImage image, int iteration) throws OcrException {
int width = image.getWidth();
int height = image.getHeight();
int bigWidth = width * 3;
int bigHeight = height * 3;
// to gray scale
BufferedImage grayscale = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
BufferedImageOp grayscaleConv = new ColorConvertOp(image.getColorModel().getColorSpace(), grayscale.getColorModel().getColorSpace(), null);
grayscaleConv.filter(image, grayscale);
// blow it up for ocr
BufferedImage newImage = new BufferedImage(bigWidth, bigHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = newImage.createGraphics();
g.drawImage(grayscale, 0, 0, bigWidth, bigHeight, null);
g.dispose();
// invert image
for (int x = 0; x < bigWidth; x++) {
for (int y = 0; y < bigHeight; y++) {
int rgba = newImage.getRGB(x, y);
Color col = new Color(rgba, true);
col = new Color(255 - col.getRed(), 255 - col.getGreen(), 255 - col.getBlue());
newImage.setRGB(x, y, col.getRGB());
}
}
// increase contrast
try {
RescaleOp rescaleOp = new RescaleOp(1.8f, -30, null);
// Source and destination are the same.
rescaleOp.filter(newImage, newImage);
} catch (Exception e) {
throw new OcrException("Error rescaling OCR image", e);
}
return newImage;
}
use of java.awt.image.RescaleOp in project spatial-portal by AtlasOfLivingAustralia.
the class PrintMapComposer method drawGoogle.
private List drawGoogle(Graphics2D g, String maptype, boolean drawTiles) {
List imageUrls = new ArrayList();
double[] resolutions = { 156543.03390625, 78271.516953125, 39135.7584765625, 19567.87923828125, 9783.939619140625, 4891.9698095703125, 2445.9849047851562, 1222.9924523925781, 611.4962261962891, 305.74811309814453, 152.87405654907226, 76.43702827453613, 38.218514137268066, 19.109257068634033, 9.554628534317017, 4.777314267158508, 2.388657133579254, 1.194328566789627, 0.5971642833948135 };
//nearest resolution
int imgSize = 640;
int gScale = 2;
double actualWidth = extents[2] - extents[0];
double actualHeight = extents[3] - extents[1];
int res = 0;
while (res < resolutions.length - 1 && resolutions[res + 1] * imgSize > actualWidth && resolutions[res + 1] * imgSize > actualHeight) {
res++;
}
int centerX = (int) ((extents[2] - extents[0]) / 2 + extents[0]);
int centerY = (int) ((extents[3] - extents[1]) / 2 + extents[1]);
double latitude = SpatialUtil.convertMetersToLat(centerY);
double longitude = SpatialUtil.convertMetersToLng(centerX);
//need to change the size requested so the extents match the output extents.
int imgWidth = (int) ((extents[2] - extents[0]) / resolutions[res]);
int imgHeight = (int) ((extents[3] - extents[1]) / resolutions[res]);
String uri = "http://maps.googleapis.com/maps/api/staticmap?";
String parameters = "center=" + latitude + "," + longitude + "&zoom=" + res + "&scale=" + gScale + "&size=" + imgWidth + "x" + imgHeight + "&maptype=" + maptype;
String key = CommonData.getSettings().getProperty("google.maps.apikey", null);
if (key != null)
parameters += "&key=" + key;
RescaleOp op = new RescaleOp(new float[] { 1f, 1f, 1f, 1f }, new float[] { 0f, 0f, 0f, 0f }, null);
LOGGER.debug("print uri: " + uri + parameters);
imageUrls.add(uri + parameters);
if (drawTiles) {
BufferedImage img = getImage(uri + parameters, true);
if (img != null) {
BufferedImage tmp = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
tmp.getGraphics().drawImage(img, 0, 0, width, height, 0, 0, imgWidth * gScale, imgHeight * gScale, null);
g.drawImage(tmp, op, 0, 0);
}
}
return imageUrls;
}
use of java.awt.image.RescaleOp in project spatial-portal by AtlasOfLivingAustralia.
the class PrintMapComposer method drawUri.
private List drawUri(Graphics2D g, String uri, float opacity, boolean imageOnly256, boolean drawTiles) {
List imageUrls = new ArrayList();
//tiles
double minX, maxX, minY, maxY, stepX, stepY;
minX = extents[0];
maxX = extents[2];
minY = extents[1];
maxY = extents[3];
int pageWidth = width;
int pageHeight = height;
int tileWidth = 1024;
int tileHeight = 1024;
if (imageOnly256) {
tileWidth = 256;
tileHeight = 256;
}
int ix;
int iy;
stepX = (maxX - minX) / pageWidth * tileWidth;
stepY = (maxY - minY) / pageHeight * tileHeight;
RescaleOp op = new RescaleOp(new float[] { 1f, 1f, 1f, opacity }, new float[] { 0f, 0f, 0f, 0f }, null);
iy = 0;
for (double y = maxY; y > minY; y -= stepY, iy++) {
ix = 0;
for (double x = minX; x < maxX; x += stepX, ix++) {
String bbox = "&BBOX=" + x + "," + (y - stepY) + "," + (x + stepX) + "," + y + "&WIDTH=" + tileWidth + "&HEIGHT=" + tileHeight + "&TRANSPARENT=true";
LOGGER.debug("print uri: " + uri + bbox);
imageUrls.add(uri + bbox);
if (drawTiles) {
BufferedImage img = getImage(uri + bbox, true);
if (img != null) {
int nx = ix * tileWidth;
int ny = iy * tileHeight;
BufferedImage tmp = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_INT_ARGB);
tmp.getGraphics().drawImage(img, 0, 0, null);
g.drawImage(tmp, op, nx, ny);
}
}
}
}
return imageUrls;
}
use of java.awt.image.RescaleOp in project spatial-portal by AtlasOfLivingAustralia.
the class PrintMapComposer method drawOSM.
private List drawOSM(Graphics2D g, boolean drawTiles) {
List imageUrls = new ArrayList();
double[] resolutions = { 156543.03390625, 78271.516953125, 39135.7584765625, 19567.87923828125, 9783.939619140625, 4891.9698095703125, 2445.9849047851562, 1222.9924523925781, 611.4962261962891, 305.74811309814453, 152.87405654907226, 76.43702827453613, 38.218514137268066, 19.109257068634033, 9.554628534317017, 4.777314267158508, 2.388657133579254, 1.194328566789627, 0.5971642833948135 };
double[] origin = { -20037508.34, -20037508.34 };
//nearest resolution
double actualRes = (extents[2] - extents[0]) / width;
int res = 0;
while (res < resolutions.length && resolutions[res] > actualRes) {
res++;
}
if (res > 0) {
res--;
}
int tileWidth = 256;
int tileHeight = 256;
int tiles = (int) Math.pow(2, res);
int sx = (int) Math.floor((extents[0] - origin[0]) / resolutions[res] / tileWidth);
int sy = tiles - (int) Math.ceil((extents[3] - origin[1]) / resolutions[res] / tileHeight);
int mx = (int) Math.ceil((extents[2] - origin[0]) / resolutions[res] / tileWidth);
int my = tiles - (int) Math.floor((extents[1] - origin[1]) / resolutions[res] / tileHeight);
if (sx < 0) {
sx = 0;
}
if (my < 0) {
my = 0;
}
if (sy >= tiles) {
sy = tiles - 1;
}
int destWidth = width;
//square tiles
int srcWidth = (int) (destWidth / (extents[2] - extents[0]) * (tileWidth * resolutions[res]));
int srcHeight = srcWidth;
int xOffset = (int) ((sx - ((extents[0] - origin[0]) / resolutions[res] / tileWidth)) * srcWidth);
int yOffset = (int) ((sy - (((-1 * origin[1]) - extents[3]) / resolutions[res] / tileHeight)) * srcHeight);
RescaleOp op = new RescaleOp(new float[] { 1f, 1f, 1f, 1f }, new float[] { 0f, 0f, 0f, 0f }, null);
String uri = "http://tile.openstreetmap.org/";
for (int iy = my; iy >= sy; iy--) {
for (int ix = sx; ix <= mx; ix++) {
String bbox = res + "/" + (ix % tiles) + "/" + iy + ".png";
LOGGER.trace("print uri: " + uri + bbox);
imageUrls.add(uri + bbox);
if (drawTiles) {
BufferedImage img = getImage(uri + bbox, true);
if (img != null) {
int nx = (ix - sx) * srcWidth + xOffset;
int ny = (iy - sy) * srcHeight + yOffset;
BufferedImage tmp = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_ARGB);
tmp.getGraphics().drawImage(img, 0, 0, srcWidth, srcHeight, 0, 0, tileWidth, tileHeight, null);
g.drawImage(tmp, op, nx, ny);
}
}
}
}
return imageUrls;
}
use of java.awt.image.RescaleOp in project poi by apache.
the class BitmapImageRenderer method setAlpha.
@Override
public void setAlpha(double alpha) {
if (img == null)
return;
Dimension dim = getDimension();
BufferedImage newImg = new BufferedImage((int) dim.getWidth(), (int) dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImg.createGraphics();
RescaleOp op = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, (float) alpha }, new float[] { 0, 0, 0, 0 }, null);
g.drawImage(img, op, 0, 0);
g.dispose();
img = newImg;
}
Aggregations