2011-12-26 13 views
13

मुझे छवियों को अलग-अलग घुमाने में सक्षम होना चाहिए (जावा में)। मुझे अभी तक मिली एकमात्र चीज़ g2d.drawImage (छवि, affinetransform, ImageObserver) है। दुर्भाग्यवश, मुझे छवि को एक विशिष्ट बिंदु पर खींचने की आवश्यकता है, और तर्क के साथ कोई विधि नहीं है कि 1. छवि को अलग से दोहराता है और 2. मुझे एक्स और वाई सेट करने की अनुमति देता है। किसी भी मदद की सराहना की जाती हैजावा: छवियों को घूर्णन

उत्तर

25

इस तरह आप इसे कर सकते हैं। यह कोड 'छवि' नामक एक बफ़र छवि के अस्तित्व को मान लिया गया है (अपनी टिप्पणी की तरह कहते हैं)

// The required drawing location 
int drawLocationX = 300; 
int drawLocationY = 300; 

// Rotation information 

double rotationRequired = Math.toRadians (45); 
double locationX = image.getWidth()/2; 
double locationY = image.getHeight()/2; 
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY); 
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); 

// Drawing the rotated image at the required drawing locations 
g2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null); 
+0

यह काम किया, धन्यवाद! – Jimmt

+2

इतना जटिल क्यों? ट्रांसफॉर्म में रोटेशन और अनुवाद दोनों होते हैं, इसलिए उत्तर से 'tx' के साथ 'g2d.drawImage (छवि, tx, ImageObserver)' करें। –

+2

धन्यवाद, लेकिन यह कुछ छवियों में कटौती करता है। – HyperNeutrino

8

AffineTransform उदाहरणों concatenated जा सकता है (एक साथ जोड़)। इसलिए आपके पास एक परिवर्तन हो सकता है जो 'शिफ्ट टू उत्पत्ति', 'घुमाएं' और 'वांछित स्थिति में वापस स्थानांतरित करें' को जोड़ता है।

+4

+1 ['RotateApp'] (http://stackoverflow.com/a/3420651/230513) एक उदाहरण है। – trashgod

0
public static BufferedImage rotateCw(BufferedImage img) 
{ 
    int   width = img.getWidth(); 
    int   height = img.getHeight(); 
    BufferedImage newImage = new BufferedImage(height, width, img.getType()); 

    for(int i=0 ; i < width ; i++) 
     for(int j=0 ; j < height ; j++) 
      newImage.setRGB(height-1-j, i, img.getRGB(i,j)); 

    return newImage; 
} 

https://coderanch.com/t/485958/java/Rotating-buffered-image

0

इस तरह के एक जटिल ड्रा बयान के उपयोग के बिना यह करने के लिए एक आसान तरीका से:

//Make a backup so that we can reset our graphics object after using it. 
    AffineTransform backup = g2d.getTransform(); 
    //rx is the x coordinate for rotation, ry is the y coordinate for rotation, and angle 
    //is the angle to rotate the image. If you want to rotate around the center of an image, 
    //use the image's center x and y coordinates for rx and ry. 
    AffineTransform a = AffineTransform.getRotateInstance(angle, rx, ry); 
    //Set our Graphics2D object to the transform 
    g2d.setTransform(a); 
    //Draw our image like normal 
    g2d.drawImage(image, x, y, null); 
    //Reset our graphics object so we can draw with it again. 
    g.setTransform(backup); 
संबंधित मुद्दे