1

私はJavaアプリケーションを使用して、ユーザーがカメラから写真を撮り、Webサービスを使用して送信できるようにしていますが、画像の送信中に問題が発生します。画像が大きいので送信に時間がかかるので画像を圧縮したい。私はしようとしました:

1- 次のコードを使用します。

Bitmap img = BitmapFactory.decodeFile("C:\\test.jpg");

ByteArrayOutputStream streem = new ByteArrayOutputStream();  
img.compress(Bitmap.CompressFormat.JPEG, 75, streem);
byte[] b = streem.toByteArray();

しかし、このコードは私の場合は役に立ちません。なぜなら、それは画像を非常に悪くし、画像サイズにあまり影響を与えないからです。

2- サイズを変更する方法について多くを検索しますが、すべての結果は BufferedImage を使用しています。多くのメモリサイズが必要なため、この型(クラス)を使用できません:

private static BufferedImage resizeImage(BufferedImage originalImage, int type)
{
    BufferedImage resizedImage = new BufferedImage(new_w, new_h, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, new_w, new_h, null);
    g.dispose();

    return resizedImage;
}

代わりにビットマップを使用したいのですが、私のアプリケーションではどんな体でも助けてくれますか???

4

2 に答える 2

1

を必要な形式ImageIOに書き込むために使用しますBufferedImage

に出力ストリームをImageIO提供できるので、ほぼどこにでも書き込むことができるはずです。

詳細については、画像の書き込み/保存を確認してください

于 2012-11-24T20:54:39.447 に答える
1

私はこれらの牽引方法を見つけました:

private static int CalculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    float height = (float) options.outHeight;
    float width = (float) options.outWidth;
    float inSampleSize = 0;

    if (height > reqHeight || width > reqWidth) {
        inSampleSize = width > height ? height / reqHeight : width
                / reqWidth;
    }

    return (int) Math.round(inSampleSize);
}

public static byte[] ResizeImage(int reqWidth, int reqHeight, byte[] buffer) {
    BitmapFactory.Options op = new Options();
    op.inJustDecodeBounds = true;

    BitmapFactory.decodeByteArray(buffer, 0, buffer.length, op);

    op.inSampleSize = CalculateInSampleSize(op, reqWidth, reqHeight);

    op.inJustDecodeBounds = false;
    try {
        return ToByte(BitmapFactory.decodeByteArray(buffer, 0,
                buffer.length, op));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}
于 2012-11-28T12:23:30.490 に答える