2

私の要件はこのようなものです。ファイル接続を使用して携帯電話からファイルを読み取り、その画像のサムネイルを作成してサーバーに投稿する必要があります。FileConnection API を使用して画像を読み取ることができ、サムネイルを作成することもできます。

サムネイルを作成した後、その画像を byte[] に戻す方法が見つかりません。出来ますか?

サムネイル変換のコード:

    private Image createThumbnail(Image image) {
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();

        int thumbWidth = 128;
        int thumbHeight = -1;

        if (thumbHeight == -1)
            thumbHeight = thumbWidth * sourceHeight / sourceWidth;

        Image thumb = Image.createImage(thumbWidth, thumbHeight);
        thumb.getGraphics();
        Graphics g = thumb.getGraphics();

        for (int y = 0; y < thumbHeight; y++) {
            for (int x = 0; x < thumbWidth; x++) {
                g.setClip(x, y, 1, 1);
                int dx = x * sourceWidth / thumbWidth;
                int dy = y * sourceHeight / thumbHeight;
                g.drawImage(image, x - dx, y - dy);
            }
        }

        Image immutableThumb = Image.createImage(thumb);

        return thumb;
    }
4

1 に答える 1

2

MIDP2.0 のImage.getRGB()はあなたの味方です。次のように、ARGB ピクセル データを int 配列として取得できます。

int w = theImage.getWidth();
int h = theImage.getHeight();
int[] argb = new int[w * h];
theImage.getRGB(argb, 0, w, 0, 0, w, h);

int 配列はImage.createRGBImage()のパラメーターとして使用できます。デスクトップ Java では、BufferedImage次のように使用できます。

BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
img.setRGB(0, 0, w, h, ints, 0, w);
于 2009-12-08T15:25:48.520 に答える