亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將圖像數據復制到 BufferedImage 的子類中?

如何將圖像數據復制到 BufferedImage 的子類中?

精慕HU 2022-01-12 14:43:04
我有一個名為 Bitmap 的類,它從 BufferedImage 擴展而來,public class Bitmap extends BufferedImage {...它的一種方法稱為 copyImage,它將源圖像中的內容復制到類中,它可以工作,但此方法不保持源圖像的原始縱橫比和尺寸。public void copyImage(Image image) {    if (image != null) {        BufferedImage bi = (BufferedImage) image;        Graphics g = getGraphics();        g.drawImage(bi, 0, 0, width, height, null);    }}我希望這個方法將源圖像復制到類中,并保持其原始縱橫比和尺寸保持不變,我想調整寬度和高度的大小我將上面的代碼修改為:public void copyImage(Image image) {    if (image != null) {        this.width = image.getWidth(null);        this.height = image.getWidth(null);        BufferedImage bi = (BufferedImage) image;        Graphics g = getGraphics();        g.drawImage(bi, 0, 0, width, height, null);    }}但它不起作用,我該如何修改上面的代碼來復制圖像?提前致謝。
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

這是錯誤的:


public void copyImage(Image image) {

    if (image != null) {

        this.width = image.getWidth(null);

        this.height = image.getWidth(null);


        BufferedImage bi = (BufferedImage) image;

        Graphics g = getGraphics();


        g.drawImage(bi, 0, 0, width, height, null);

    }

}

你的主要問題是:

  1. 您似乎正在嘗試更改原始圖像的固有寬度和高度,this圖像,您不應該這樣做,而不是這樣

  2. 您正在將參數圖像的寬度分配給this.height字段this.height = image.getWidth(null);

其他事宜:

  • 你沒有節省資源

  • 你正在制作危險且不必要的演員表

它應該是

public void copyImage(Image image) {

    if (image != null) {

        // don't change the width/height of your original image

        int width = image.getWidth(null);

        // int height = image.getWidth(null);

        int height = image.getHeight(null); // *** Note change ***


        // BufferedImage bi = (BufferedImage) image;  // *** no need ***

        Graphics g = getGraphics();


        g.drawImage(image, 0, 0, width, height, null);

        g.dispose();  // save resources

    }

}   

使用顯示概念證明的MCVE測試代碼:


import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.net.URL;


import javax.imageio.ImageIO;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;


public class TestImage {

    public static final String SOMME_PATH = "https://upload.wikimedia.org/"

            + "wikipedia/commons/thumb/f/fa/Cheshire_Regiment_trench_Somme_1916.jpg"

            + "/1024px-Cheshire_Regiment_trench_Somme_1916.jpg";

    public static final String BATTLE_PATH = "https://upload.wikimedia.org/wikipedia/"

            + "commons/1/13/K%C3%A4mpfe_auf_dem_Doberdo.JPG";


    public static void main(String[] args) {

        int imgW = 1000;

        int imgH = 700;

        MyImage myImage = new MyImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);

        BufferedImage sommeTrench = null;

        BufferedImage battleOfDoberdò = null;


        try {

            URL url = new URL(SOMME_PATH);

            sommeTrench = ImageIO.read(url);


            url = new URL(BATTLE_PATH);

            battleOfDoberdò = ImageIO.read(url);

        } catch (IOException e) {

            e.printStackTrace();

            System.exit(-1);

        }


        Icon icon = new ImageIcon(myImage);

        JOptionPane.showMessageDialog(null, icon, "Original MyImage", JOptionPane.PLAIN_MESSAGE);


        myImage.copyImage(sommeTrench);

        icon = new ImageIcon(myImage);

        JOptionPane.showMessageDialog(null, icon, "MyImage with Somme Trench", JOptionPane.PLAIN_MESSAGE);


        myImage.copyImage(battleOfDoberdò);        

        icon = new ImageIcon(myImage);

        JOptionPane.showMessageDialog(null, icon, "MyImage with Battle Of Doberdò", JOptionPane.PLAIN_MESSAGE);


    }

}

class MyImage extends BufferedImage {


    public MyImage(int width, int height, int imageType) {

        super(width, height, imageType);

    }


    public void copyImage(Image image) {

        if (image != null) {

            int width = image.getWidth(null);


            int height = image.getHeight(null); // *** Note change ***


            Graphics g = getGraphics();


            g.drawImage(image, 0, 0, width, height, null);

            g.dispose(); // save resources

        }

    }    

}

如果您運行此代碼,您將看到 3 個圖像在 3 個 JOptionPanes 中顯示為 ImageIcons,第一個是原始空白 MyImage 對象,然后在第一次世界大戰中的 2 個圖像被復制到原始圖像中。


查看完整回答
反對 回復 2022-01-12
  • 1 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號