منتدى مصر التقني
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.

كيفية تغيير حجم الصور في java

اذهب الى الأسفل

كيفية تغيير حجم الصور في java Empty كيفية تغيير حجم الصور في java

مُساهمة من طرف زهراء الثلاثاء نوفمبر 06, 2018 12:15 pm

في Java يمكنك تغيير حجم (أو قياس) صورة مقروءة من ملف وحفظ الصورة بعد تغير حجمها كملف اخر ، يمكننا اتباع هذه الخطوات:

          نقوم بإنشاء الكائن BufferedImage لقراءة الصورة المدخلة عن طريق استدعاء الطريقة read(File)للفئة ImageIO.
          ايضا سوف نستخدم الكائن BufferedImage لتحديد العرض والارتفاع المطلوب.
          رسم الصورة المدخلة فى الكائن BufferedImage باستخدام الكائن  Graphics2D .
           بحفظ المخرجة إلى ملف على القرص باستخدام طريقة write(File) لفئة ImageIO.
 

واليكم مثال ينفذ طريقتين لتغيير حجم صورة وحفظ النتيجة إلى ملف صورة آخر

الكود:

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;

public class ImageResizer {
public static void resize(String inputImagePath,
            String outputImagePath, int scaledWidth, int scaledHeight)
            throws IOException {
        // reads input image
        File inputFile = new File(inputImagePath);
        BufferedImage inputImage = ImageIO.read(inputFile);
 
        // creates output image
        BufferedImage outputImage = new BufferedImage(scaledWidth,
                scaledHeight, inputImage.getType());
 
        // scales the input image to the output image
        Graphics2D g2d = outputImage.createGraphics();
        g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
        g2d.dispose();
 
        // extracts extension of output file
        String formatName = outputImagePath.substring(outputImagePath
                .lastIndexOf(".") + 1);
 
        // writes to output file
        ImageIO.write(outputImage, formatName, new File(outputImagePath));
    }
 

    public static void resize(String inputImagePath,
            String outputImagePath, double percent) throws IOException {
        File inputFile = new File(inputImagePath);
        BufferedImage inputImage = ImageIO.read(inputFile);
        int scaledWidth = (int) (inputImage.getWidth() * percent);
        int scaledHeight = (int) (inputImage.getHeight() * percent);
        resize(inputImagePath, outputImagePath, scaledWidth, scaledHeight);
    }
 
    /**
     * Test resizing images
     */
    public static void main(String[] args) {
        String inputImagePath = "D:/Photo/Puppy.jpg";
        String outputImagePath1 = "D:/Photo/Puppy_Fixed.jpg";
        String outputImagePath2 = "D:/Photo/Puppy_Smaller.jpg";
        String outputImagePath3 = "D:/Photo/Puppy_Bigger.jpg";
 
        try {
            // resize to a fixed width (not proportional)
            int scaledWidth = 1024;
            int scaledHeight = 768;
            ImageResizer.resize(inputImagePath, outputImagePath1, scaledWidth, scaledHeight);
 
            // resize smaller by 50%
            double percent = 0.5;
            ImageResizer.resize(inputImagePath, outputImagePath2, percent);
 
            // resize bigger by 50%
            percent = 1.5;
            ImageResizer.resize(inputImagePath, outputImagePath3, percent);
 
        } catch (IOException ex) {
            System.out.println("Error resizing the image.");
            ex.printStackTrace();
        }
    }
 
}
زهراء
زهراء
........
........

تاريخ التسجيل : 18/02/2011
المساهمات : 438
النقاط : 769
التقيم : 67
الدولة : مصر
الجنس : انثى

الرجوع الى أعلى الصفحة اذهب الى الأسفل

كيفية تغيير حجم الصور في java Empty رد: كيفية تغيير حجم الصور في java

مُساهمة من طرف زهراء الثلاثاء نوفمبر 06, 2018 12:26 pm

ايضا يمكن تغير حجم الصورة با ستخدام الشفرة التالية :

الكود:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {

  public void paint(Graphics g) {
    Image img1 = Toolkit.getDefaultToolkit().getImage("yourFile.gif");

    int width = img1.getWidth(this);
    int height = img1.getHeight(this);

    int scale = 2;
    int w = scale * width;
    int h = scale * height;
    // explicitly specify width (w) and height (h)
    g.drawImage(img1, 10, 10, (int) w, (int) h, this);

  }
}

public class Graphics2DDrawScaleImage {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}
زهراء
زهراء
........
........

تاريخ التسجيل : 18/02/2011
المساهمات : 438
النقاط : 769
التقيم : 67
الدولة : مصر
الجنس : انثى

الرجوع الى أعلى الصفحة اذهب الى الأسفل

الرجوع الى أعلى الصفحة

ََ

مواضيع ذات صلة


 
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى