Anasayfa

C ve Sistem Programcıları Derneği

  • CSD Hakkında
    • Yönetim Kurulu
    • Referanslar
    • Üyelik
    • Adres
    • Kroki
    • İletişim
  • Kurslar
    • Kurs Başvurusu
    • Eğitmenler
  • Seminerler
    • Seminer İste
    • Seminer Ver
  • Kaynaklar
    • Yararlı Linkler
    • Kaynak Kod Arşivi
    • Makaleler
    • Çalışma Soruları
    • Yararlı Kitaplar
    • Dosya Arşivi
    • Döküman Arşivi
  • Duyurular
  • Haberler
  • İş İlanları
    • İş İlanı Gönder
  • SSS
  • Kurs Başvurusu

menü

  • CSD Hakkında
    • Yönetim Kurulu
    • Referanslar
    • Üyelik
    • Adres
    • Kroki
    • İletişim
  • Kurslar
    • Kurs Başvurusu
    • Eğitmenler
  • Seminerler
    • Seminer İste
    • Seminer Ver
  • Kaynaklar
    • Yararlı Linkler
    • Kaynak Kod Arşivi
    • Makaleler
    • Çalışma Soruları
    • Yararlı Kitaplar
    • Dosya Arşivi
    • Döküman Arşivi
  • Duyurular
  • Haberler
  • İş İlanları
    • İş İlanı Gönder
  • SSS
  • Kurs Başvurusu

e-posta ile bilgilendirme.

Derneğimizdeki etkinliklerden haberdar olun!

Haber mektubu üyeliğiniz

Kimler çevrimiçi

Şu an 0 kullanıcı ve 1 ziyaretçi çevrimiçi.
""
dreamhost
Anasayfa

Java'da Resimlerin Döndürülmesi

10.06.2011 - 16:24 tarihinde aykut gönderdi

Java dilinde BufferedImage sınıfıyla temsil edilen resimlerin döndürülmesi (rotate) işlemi. Aykut Taşdelen

Kaynak kod: 
public class ImageUtil
{
	public static BufferedImage rotateImage(BufferedImage img, double degree)
	{
		BufferedImage bufImg = toBufferedImage(img);
		double angle = Math.toRadians(degree);
		return tilt(bufImg, angle);
	}
 
	public static BufferedImage tilt(BufferedImage image, double angle)
	{
		double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
		int w = image.getWidth(), h = image.getHeight();
 
		int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
 
		GraphicsConfiguration gc = getDefaultConfiguration();
		BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
		Graphics2D g = result.createGraphics();
		g.translate((neww - w) / 2, (newh - h) / 2);
		g.rotate(angle, w / 2, h / 2);
		g.drawRenderedImage(image, null);
		g.dispose();
		return result;
	}
 
	 public static GraphicsConfiguration getDefaultConfiguration() {
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
         GraphicsDevice gd = ge.getDefaultScreenDevice();
         return gd.getDefaultConfiguration();
	 }  
 
	 public static BufferedImage toBufferedImage(Image image) {
         if (image instanceof BufferedImage) {
             return (BufferedImage)image;
         }
         // piksel kitleme
         image = new ImageIcon(image).getImage();
         boolean hasAlpha = hasAlpha(image);
         BufferedImage bimage = null;
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
 
         try {
             int transparency = Transparency.OPAQUE;
             if (hasAlpha) {
                 transparency = Transparency.BITMASK;
             }
             GraphicsDevice gs = ge.getDefaultScreenDevice();
             GraphicsConfiguration gc = gs.getDefaultConfiguration();
             bimage = gc.createCompatibleImage(
             image.getWidth(null), image.getHeight(null), transparency);  
 
         } catch (HeadlessException e) {
         }  
 
         if (bimage == null) {
             int type = BufferedImage.TYPE_INT_RGB;
             if (hasAlpha) {  
 
                 type = BufferedImage.TYPE_INT_ARGB;  
 
             }  
 
             bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
         }
         Graphics g = bimage.createGraphics();
         g.drawImage(image, 0, 0, null);
         g.dispose();
         return bimage;
     }  
 
	 public static boolean hasAlpha(Image image) {
         if (image instanceof BufferedImage) {
             BufferedImage bimage = (BufferedImage)image;
             return bimage.getColorModel().hasAlpha();
         }  
 
          PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
          try {
             pg.grabPixels();
         } catch (InterruptedException e) {  
 
         }
         ColorModel cm = pg.getColorModel();
         return cm.hasAlpha();
     }
 }
  • Yazıcı uyumluYazıcı uyumlu
  • Arkadaşa gönderArkadaşa gönder
  • PDFPDF
Etiketler:
  • Java
  • bufferedimage
  • döndürme
  • image
  • java
  • rotate
  • rotation
  • swing

Adres: 2.Taşocağı Cd. Oğuz Sk. Barbaros Apt. No: 11/4 PK: 34387  Mecidiyeköy / İSTANBUL Tel: (212) 274 63 60  (212) 274 99 89  (212) 275 88 97  (212) 288 35 20 Fax: (212) 275 88 97 E-Posta: csystem@csystem.org

İçerik yayınları
Fervens Drupal theme by Leow Kah Thong. Designed by Design Disease and brought to you by Smashing Magazine.