[英]Black area using java 2D graphics rotation?
我將首先展示出錯的例子然后我將解釋如何,最后我會問我的問題。
這是我要旋轉的圖片。
我將它旋轉90度和270度,多次然后合並
那些成為一個大的緩沖圖像。
我用來旋轉單個bufferedImage的代碼是這樣的:
public static BufferedImage rotate(BufferedImage img, int angle) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(w, h, img.getType());
Graphics2D g = dimg.createGraphics();
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawImage(img, null, 0, 0);
return dimg;
}
輪換的出現看起來像這樣。
這些黑條的原因是因為在代碼中您可以清楚地看到我創建了一個單獨的緩沖圖像,它將是最終圖像。
其中使用原始寬度和高度,因為圖像旋轉了with和height開關所以我通過改變BufferedImage dimg = new BufferedImage(w, h, img.getType());
補償這個問題: BufferedImage dimg = new BufferedImage(w, h, img.getType());
to BufferedImage dimg = new BufferedImage(h, w, img.getType());
。
我認為這可以解決我的問題。
但我現在錯了,旋轉結果就是這個。
所以從這一點開始我就不知道為什么要這樣做了。
我可能只是俯視一個小東西,或者即使我找不到它也是一個常見的錯誤
任何這種情況的發生。
所以這是我的問題,為什么這樣做? 我該如何解決這個問題。
圖像不是方形的。 如果將其旋轉90°,則會創建需要填充的間隙。
解決方案:
我想到了。
我在開始時做的事情是旋轉主圖像(dimg),
然后將原始圖像繪制到它。
我也可以試着在我早期的旋轉中將一個正方形放在圓圈中
沒有任何意義。
所以我需要做的是首先創建主機,將圖像繪制到主機,旋轉
托管並將其作為最終圖像返回。
public static BufferedImage rotate(BufferedImage img, int angle) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(w, h, img.getType());
Graphics2D g = dimg.createGraphics();
g.drawImage(img, null, 0, 0); //Draw before rotating
g.rotate(Math.toRadians(angle), w/2, h/2); //Rotating after drawing
return dimg;
}
我希望這也可以幫助其他人
如果你想使用類似的代碼作為第一個代碼,這可能會有所幫助(如果刪除注釋和調試行(如繪制背景),它只有translate((Ww)/ 2,(Hh)/ 2)行另外)
// do not forget to import static java.lang.Math.*
public static BufferedImage rotate(BufferedImage img, int angle) {
int w = img.getWidth(null);
int h = img.getHeight(null);
double rad = toRadians(angle);
double eps = 1e-3;
int W=(int)(abs(cos(rad))*w+abs(sin(rad))*h-eps)+1;//W after rotation(calculated by using a little geometry )
int H=(int)(abs(sin(rad))*w+abs(cos(rad))*h-eps)+1;//H after rotation
//you may use max value ( diameter of the rectangle ) instead of dynamic value but in that case you must be careful of the black edges ( in this case red edges )
// if 90 is not a divisor of angle then you can't fit a rectangle with that angle in another one so the red edges are inevitable
// but with calculated W and H this edges are minimum
BufferedImage dimg = new BufferedImage(W,H, BufferedImage.TYPE_INT_RGB);// you can change it to any type you want it's just a sample
Graphics2D g = dimg.createGraphics();
g.setColor(Color.RED); // background color of red for displaying the red edges when image is not completely fit
g.fillRect(0, 0, W, H);
int x=(W-w)/2;
int y=(H-h)/2;
g.translate(x, y); // moving dimg center to img center ( this was what first code lack in )
g.rotate(-rad, w/2, h/2); // now rotating dimg around the center of img ( which is now same as center of dimg )
// we rotate dimg by -rad and draw img normally , it's like rotating img by rad instead of dimg by -rad
g.drawImage(img,null,0,0); // and drawing
return dimg;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.