use of java.awt.image.AffineTransformOp in project digilib by robcast.
the class ImageLoaderDocuImage method mirror.
/*
* (non-Javadoc)
* @see digilib.image.DocuImageImpl#mirror(double)
*/
public void mirror(double angle) throws ImageOpException {
logger.debug("mirror: " + angle);
// setup mirror
double mx = 1;
double my = 1;
double tx = 0;
double ty = 0;
if (Math.abs(angle - 0) < epsilon) {
// 0 degree
mx = -1;
tx = img.getWidth();
} else if (Math.abs(angle - 90) < epsilon) {
// 90 degree
my = -1;
ty = img.getHeight();
} else if (Math.abs(angle - 180) < epsilon) {
// 180 degree
mx = -1;
tx = img.getWidth();
} else if (Math.abs(angle - 270) < epsilon) {
// 270 degree
my = -1;
ty = img.getHeight();
} else if (Math.abs(angle - 360) < epsilon) {
// 360 degree
mx = -1;
tx = img.getWidth();
} else {
logger.error("invalid mirror angle " + angle);
return;
}
AffineTransformOp mirOp = new AffineTransformOp(new AffineTransform(mx, 0, 0, my, tx, ty), renderHint);
img = mirOp.filter(img, null);
// invalidate image size
imageSize = null;
}
use of java.awt.image.AffineTransformOp in project digilib by robcast.
the class ImageLoaderDocuImage method scale.
/*
* (non-Javadoc)
* @see digilib.image.DocuImageImpl#scale(double, double)
*/
public void scale(double scaleX, double scaleY) throws ImageOpException {
logger.debug("scale: " + scaleX);
/*
* for downscaling in high quality the image is blurred first ...
*/
if ((scaleX <= 0.5) && (quality > 1)) {
int bl = (int) Math.floor(1 / scaleX);
blur(bl);
}
/*
* ... then scaled.
*
* We need to correct the scale factors to round to whole pixels
* or else we get a 1px black (or transparent) border.
*/
int imgW = img.getWidth();
int imgH = img.getHeight();
double targetW = imgW * scaleX;
double targetH = imgH * scaleY;
double deltaX = targetW - Math.floor(targetW);
double deltaY = targetH - Math.floor(targetH);
if (deltaX > epsilon) {
// round x
if (deltaX > 0.5d) {
logger.debug("rounding up x scale factor");
scaleX += (1 - deltaX) / imgW;
} else {
logger.debug("rounding down x scale factor");
scaleX -= deltaX / imgW;
}
}
if (deltaY > epsilon) {
// round y
if (deltaY > 0.5d) {
logger.debug("rounding up y scale factor");
scaleY += (1 - deltaY) / imgH;
} else {
logger.debug("rounding down y scale factor");
scaleY -= deltaY / imgH;
}
}
// scale with AffineTransformOp
logger.debug("scaled from " + imgW + "x" + imgH + " img=" + img);
AffineTransformOp scaleOp = new AffineTransformOp(AffineTransform.getScaleInstance(scaleX, scaleY), renderHint);
BufferedImage dest = null;
if (imageHacks.get(Hacks.setDestForScale)) {
// keep image type unless we know its unsuitable
int imgType = img.getType();
if (imgType != BufferedImage.TYPE_CUSTOM && imgType != BufferedImage.TYPE_BYTE_BINARY && imgType != BufferedImage.TYPE_BYTE_INDEXED) {
// fix destination image
logger.debug("scale: fixing destination image type");
int dw = (int) Math.round(imgW * scaleX);
int dh = (int) Math.round(imgH * scaleY);
dest = new BufferedImage(dw, dh, imgType);
}
}
img = scaleOp.filter(img, dest);
logger.debug("scaled to " + img.getWidth() + "x" + img.getHeight() + " img=" + img);
// invalidate image size
imageSize = null;
}
use of java.awt.image.AffineTransformOp in project nutz by nutzam.
the class Images method rotate.
/**
* 对一个图像进行旋转
*
* @param image
* 图像
* @param degree
* 旋转角度, 90 为顺时针九十度, -90 为逆时针九十度
* @return 旋转后得图像对象
*/
public static BufferedImage rotate(BufferedImage image, int degree) {
// 原始图象的宽度
int iw = image.getWidth();
// 原始图象的高度
int ih = image.getHeight();
int w = 0;
int h = 0;
int x = 0;
int y = 0;
degree = degree % 360;
if (degree < 0) {
// 将角度转换到0-360度之间
degree = 360 + degree;
}
// 将角度转为弧度
double ang = degree * 0.0174532925;
if (degree == 180 || degree == 0 || degree == 360) {
w = iw;
h = ih;
} else if (degree == 90 || degree == 270) {
w = ih;
h = iw;
} else {
int d = iw + ih;
w = (int) (d * Math.abs(Math.cos(ang)));
h = (int) (d * Math.abs(Math.sin(ang)));
}
// 确定原点坐标
x = (w / 2) - (iw / 2);
y = (h / 2) - (ih / 2);
BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());
Graphics2D gs = rotatedImage.createGraphics();
// 以给定颜色绘制旋转后图片的背景
gs.fillRect(0, 0, w, h);
AffineTransform at = new AffineTransform();
// 旋转图象
at.rotate(ang, w / 2, h / 2);
at.translate(x, y);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
op.filter(image, rotatedImage);
image = rotatedImage;
return image;
}
use of java.awt.image.AffineTransformOp in project yyl_example by Relucent.
the class AffineTransformTest method scale.
// 缩放
private static BufferedImage scale(BufferedImage src, int w, int h, double scale) {
double sx = w * scale / src.getWidth();
double sy = h * scale / src.getHeight();
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(sx, sy), null);
return op.filter(src, null);
}
use of java.awt.image.AffineTransformOp in project wechat by dllwh.
the class OperateImageHelper method scale2.
/**
* 缩放图像,按照长宽缩放
*
* @param srcImageFile
* @param result
* @param height
* 变换后的高度
* @param width
* 变换后的长度
* @param bb
* 比例不对称时,是否补白,true 补白;false 不补白
* @return
* @date 2016-3-30下午2:44:37
*/
public static boolean scale2(String srcImageFile, String result, int height, int width, boolean bb) {
try {
// 缩放比例
double ratio = 0.0;
File file = new File(srcImageFile);
// 读入文件
BufferedImage bi = ImageIO.read(file);
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = (ImageReader) readers.next();
ImageInputStream iis = ImageIO.createImageInputStream(file);
reader.setInput(iis, true);
// 得到源图宽
int width1 = reader.getWidth(0);
// 得到源图长
int height1 = reader.getHeight(0);
@SuppressWarnings("static-access") Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 计算比例
if ((height1 > height) || (width1 > width)) {
if (height1 > width1) {
ratio = (new Integer(height)).doubleValue() / height1;
} else {
ratio = (new Integer(width)).doubleValue() / width1;
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {
// 补白
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null)) {
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
} else {
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
}
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Aggregations