5

画像のサイズをカーテンサイズに変更する目的で使用しているコードスニペットがあります(解像度を200 dpiなどに変更したい)。基本的に必要なのは、ユーザーが選んだ画像(やや大きい)を表示したいのですが、ユーザーが承認した場合は、同じ画像を別の場所に、より低い解像度で表示したいからです。残念ながら、大きな画像を表示すると、画面に何も表示されません。また、私が変更した場合

imageLabel.setIcon(newIcon); 

imageLabel.setIcon(icon); 

画像は表示されますが、正しい解像度ではありません。そのため、このコードのスニッパーの内部に問題があり、他の場所には問題がないことがわかります。

Image img = icon.getImage();

BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
boolean myBool = g.drawImage(img, 0, 0, 100, 100, null);
System.out.println(myBool);
ImageIcon newIcon = new ImageIcon(bi);
imageLabel.setIcon(newIcon);
submitText.setText(currentImagePath);
imageThirdPanel.add(imageLabel);
4

4 に答える 4

9

画像のスケーリングの詳細を気にする必要はありません。getScaledInstance(int width, int height, int hints)Imageクラスには、この目的のために設計されたメソッドがすでにあります。Javaのドキュメントによると:

この画像の拡大縮小バージョンを作成します。デフォルトで指定された幅と高さで画像をレンダリングする新しいImageオブジェクトが返されます。元のソースイメージがすでに完全にロードされている場合でも、新しいImageオブジェクトは非同期でロードされる場合があります。幅または高さのいずれかが負の数の場合、元の画像の寸法のアスペクト比を維持するために値が置き換えられます。

そして、あなたはそれをこのように使うことができます:

// Scale Down the original image fast
Image scaledImage = imageToScale.getScaledInstance(newWidth, newHighth, Image.SCALE_FAST);
// Repaint this component
repaint();

完全な例については、これを確認してください。

于 2011-11-27T08:39:15.097 に答える
6

これが私の解決策です:

    private BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) throws IOException {  
        BufferedImage resizedImage = new BufferedImage(width, height, type);  
        Graphics2D g = resizedImage.createGraphics();  
        g.drawImage(originalImage, 0, 0, width, height, null);  
        g.dispose();  
        return resizedImage;  
    }  
于 2011-11-27T08:32:17.060 に答える
1

このコードを試して画像のサイズを変更してください:

public static Image scaleImage(Image original, int newWidth, int newHeight) {
    //do nothing if new and old resolutions are same
    if (original.getWidth() == newWidth && original.getHeight() == newHeight) {
        return original;
    }

    int[] rawInput = new int[original.getHeight() * original.getWidth()];
    original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
    int[] rawOutput = new int[newWidth * newHeight];
    // YD compensates for the x loop by subtracting the width back out
    int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth();
    int YR = original.getHeight() % newHeight;
    int XD = original.getWidth() / newWidth;
    int XR = original.getWidth() % newWidth;
    int outOffset = 0;
    int inOffset = 0;
    for (int y = newHeight, YE = 0; y > 0; y--) {
        for (int x = newWidth, XE = 0; x > 0; x--) {
            rawOutput[outOffset++] = rawInput[inOffset];
            inOffset += XD;
            XE += XR;
            if (XE >= newWidth) {
                XE -= newWidth;
                inOffset++;
            }
        }
        inOffset += YD;
        YE += YR;
        if (YE >= newHeight) {
            YE -= newHeight;
            inOffset += original.getWidth();
        }
    }
    return Image.createRGBImage(rawOutput, newWidth, newHeight, false);
}
于 2011-11-27T07:17:42.973 に答える
0

別の例をここに示します:

2D-Graphics / LoadImageandscaleit.htm "> http://www.java2s.com/Tutorial/Java/0261_2D-Graphics/LoadImageandscaleit.htm

http://www.java2s.com/Code/JavaAPI/java.awt/ImagegetScaledInstanceintwidthintheightinthints.htm

于 2011-11-27T10:18:58.360 に答える