📄 java2dtools.java
字号:
* @param keepAspectRatio if true, the source image is fit inot the given boundary keeping the aspect ration
* @param defaultImage image to use if the source image can not be read or is an invalid image
* @return
*/
public Image scaleImage(String filename, int width, int heigh, boolean keepAspectRatio, Image defaultImage) {
Image returnValue = defaultImage;
try {
Image image = null;
if (filename.equals(""))
image = defaultImage;
else {
if (FileTools.existsFile(filename)) {
image = ImageIO.read(new File(filename));
} else {
cat.warn("Image at \"" + filename + "\" does not exist");
image = defaultImage;
}
}
if (image == null) {
return returnValue;
}
int h = image.getHeight(null);
int w = image.getWidth(null);
int MIN_IMAGE_SIZE = 5;
if (h < MIN_IMAGE_SIZE || w < MIN_IMAGE_SIZE || filename.equals("")) {
image = defaultImage;
}
int scaleX = 0;
int scaleY = 0;
int scaleWidth = 0;
int scaleHeigh = 0;
if (keepAspectRatio) {
Rectangle2D window = new Rectangle(0, 0, width, heigh);
Rectangle2D imageSize = new Rectangle(0, 0, w, h);
Rectangle2D newSize = fitToWindow(window, imageSize);
scaleX = (int) newSize.getX();
scaleY = (int) newSize.getY();
scaleWidth = (int) newSize.getWidth();
scaleHeigh = (int) newSize.getHeight();
} else {
scaleX = 0;
scaleY = 0;
scaleWidth = width;
scaleHeigh = heigh;
}
//showImage(image);
BufferedImage img = new BufferedImage(width, heigh, BufferedImage.TYPE_INT_RGB);
Graphics g2 = img.getGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, width, heigh);
g2.drawImage(image, scaleX, scaleY, scaleWidth, scaleHeigh, null);
ImageFilter filter = new ImageFilter();
JLabel i = new JLabel();
returnValue = i.createImage(new FilteredImageSource(img.getSource(), filter));
//showImage(image);
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
/**
* extract dominant colors using a separate dialog
*/
public IColorRectangle[] getDominantColor(BufferedImage image1, JPanel drawPanel1) {
IColorRectangle[] returnValue = new IColorRectangle[0];
if (image1 == null) {
cat.error("Image is null. No colors are extracted");
return returnValue;
}
try {
Frame dummyFrame = new Frame();
DominantColorPlugin dominantColor = new DominantColorPlugin(dummyFrame, image1);
RGBColorPercentagePairList list = dominantColor.getResult();
if (list == null) {
return returnValue; //---------------- EXIT POINT ------------------------
}
returnValue = new IColorRectangle[list.size()];
int posX = 0;
int posY = 0;
int drawSize = drawPanel1.getWidth() * drawPanel1.getHeight();
float ratio = ((float) drawPanel1.getWidth() / drawPanel1.getHeight()); // r = w / h
int i = 0;
for (Iterator it = list.iterator(); it.hasNext(); i++) {
RGBColorPercentagePair data = (RGBColorPercentagePair) it.next();
/* TODO: use this, if you want the size depending on the draw area size
int width = (int) Math.sqrt((drawSize * data.getPercentage()) / ratio);
int heigh = (int) (width * ratio);
if (width < COMPONENT_MIN_SIZE) {
width = COMPONENT_MIN_SIZE;
}
if (heigh < COMPONENT_MIN_SIZE) {
heigh = COMPONENT_MIN_SIZE;
}*/
int width = COMPONENT_MIN_SIZE;
int heigh = (int) (width * 1.33);
System.out.println("Color:[" + data.getColor().getRed() + ", " +
data.getColor().getGreen() + ", " +
data.getColor().getBlue() + "], " +
"Percent: " + data.getPercentage() + ", x: " + posX + ", y: " + posY +
", Width: " + width + ", Heigh: " + heigh + ", Panel: " + drawPanel1.getWidth() + ", " +
drawPanel1.getHeight());
//ColorComponent component = new ColorComponent(drawPanel, 10, posY, width, heigh, data.getColor(), Color.WHITE);
//addColorComponent(component);
IColorRectangle component = new IColorRectangle(drawPanel1, posX, posY, width, heigh,
data.getColor(), Color.WHITE, false);
returnValue[i] = component;
//addNewComponent(component);
// position the components from left to right and up to down
/*posY += (heigh + 10);
if (posY > (drawPanel1.getY() - 15)) {
posX += drawPanel1.getWidth() / 3;
posY = 0;
}*/
posX += drawPanel1.getWidth() / 3;
if (posX > (drawPanel1.getWidth() - 15)) {
posY += drawPanel1.getHeight() / 3;
posX = 0;
}
}
// swap the compoent order so the biggest component do not cover the smaller ones
IColorRectangle[] swapList = new IColorRectangle[returnValue.length];
for (int j = 0; j < returnValue.length; j++) {
swapList[j] = returnValue[returnValue.length - j - 1];
}
returnValue = swapList;
//repaint();
} catch (Exception e1) {
cat.error(e1);
e1.printStackTrace();
}
return returnValue;
}
/**
* returns the dominant color of the given image in the given shape (Point() list).
* "null" is returned, if no color can be extracted
*/
public Color getDominantColor(BufferedImage image1, java.util.List pointList1) {
Color returnValue = null;
Rectangle shapeBorder;
shapeBorder = (Rectangle) calculateBorder(pointList1, 0, new Rectangle(0, 0, 0, 0));
Point leftUpPoint = new Point((int) shapeBorder.getX(), (int) shapeBorder.getY());
Point rightDownPoint = new Point((int) (shapeBorder.getX() + shapeBorder.getWidth()),
(int) (shapeBorder.getY() + shapeBorder.getHeight()));
try {
RGBColorPercentagePairList colorList = new RGBColorPercentagePairList();
DominantColorFinder colorFinder = new DominantColorFinder(image1.getRaster(), colorList,
leftUpPoint, rightDownPoint);
colorFinder.run();
if (colorList != null && colorList.size() > 0) {
RGBColorPercentagePair data = (RGBColorPercentagePair) colorList.get(0);
returnValue = (Color) data.getColor();
}
} catch (Exception e) {
cat.error(e);
e.printStackTrace();
}
return returnValue;
}
private int strahlenSatz(int x1, int x2, int y2) {
return (int) (((double) x1 * y2) / x2);
}
/**
* returns the point where the line from the given point to the
* middle of the rectangle tangents the rectangle border
*/
public Point getRectangleLineCut(Rectangle rect1, Point p1) {
Point returnValue = new Point(0, 0);
if (rect1 == null || p1 == null) {
return returnValue;
}
try {
// rectangle middle point
int middleX = (int) (rect1.getX() + (rect1.getWidth() / 2));
int middleY = (int) (rect1.getY() + (rect1.getHeight() / 2));
returnValue = new Point(middleX, middleY);
//Console.getReference().echo("Middle: " + middleX + ", " + middleY + ", P: " + p1.toString());
// Strahlensatz
// first try left or right corner
int x1 = (int) ((rect1.getWidth() / 2));
int x2 = (int) (p1.getX() - middleX);
int y2 = (int) (p1.getY() - middleY);
int y1 = strahlenSatz(x1, x2, y2);
Point borderPoint = null;
if (p1.getX() > middleX) { //first try right border
borderPoint = new Point(middleX + x1, middleY + y1);
} else { //otherwise use the left corner
borderPoint = new Point(middleX - x1, middleY - y1);
}
Rectangle smallerRect = new Rectangle((int) rect1.getX() + 2, (int) rect1.getY() + 2,
(int) rect1.getWidth() - 4, (int) rect1.getHeight() - 4);
if (smallerRect.contains(borderPoint)) { // if inside return the middle-point
//Console.getReference().echo("is inside, " + smallerRect.toString() + ", " + borderPoint.toString());
return returnValue;
}
//Console.getReference().echo("Rect y: " + (rect1.getHeight() / 2) + ", Y: " + y1 + ", " + Math.abs(y1));
if ((rect1.getHeight() / 2) >= Math.abs(y1)) { // check that the right border was calculated
returnValue = borderPoint;
return returnValue;
}
// no the line must tangent the upper or lower border
y1 = (int) ((rect1.getHeight() / 2));
x2 = (int) (p1.getX() - middleX);
y2 = (int) (p1.getY() - middleY);
x1 = strahlenSatz(x2, y2, y1);
borderPoint = null;
if (p1.getY() > middleY) { //first try the lower border
borderPoint = new Point(middleX + x1, middleY + y1);
} else { //otherwise use the upper corner
borderPoint = new Point(middleX - x1, middleY - y1);
}
/*if (smallerRect.contains(borderPoint)) { // if inside the rectangle return the middle-point
//Console.getReference().echo("is inside, " + smallerRect.toString() + ", " + borderPoint.toString());
return returnValue;
}*/
//Console.getReference().echo("Rect: " + (rect1.toString()) + ", P: " + borderPoint.toString());
if ((rect1.getWidth() / 2) >= Math.abs(x1)) { // check that the right border was calculated
returnValue = borderPoint;
return returnValue;
}
} catch (Exception e) {
cat.error(e);
}
return returnValue;
}
public Shape pointListToShape(java.util.List pointList) {
Shape returnValue = new Rectangle(0, 0, 0, 0);
try {
Polygon polygon = new Polygon();
for (Iterator pointIterator = pointList.iterator(); pointIterator.hasNext();) {
Point point = (Point) pointIterator.next();
polygon.addPoint((int) point.getX(), (int) point.getY());
}
returnValue = polygon;
} catch (Exception e) {
e.printStackTrace();
cat.error(e);
}
return returnValue;
}
/**
* calculate the surronding border of a list of Point()
* dotSize in pixel: border is calculated to be outside the dots
* if not border can be calculated, rectangel with zero dimension is returned
*/
public Rectangle2D calculateBorder(java.util.List pointList1, int dotSize1, Rectangle2D defaultRectangle) {
Rectangle2D returnValue = defaultRectangle;
if (pointList1 == null || pointList1.size() < 1) {
return returnValue;
}
Point2D typePoint = (Point2D) pointList1.get(0);
try {
Point2D left = (Point2D) typePoint.clone();
left.setLocation(0, 0);
Point2D right = (Point2D) typePoint.clone();
right.setLocation(0, 0);
Point2D up = (Point2D) typePoint.clone();
up.setLocation(0, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -