0

インターネットから URL 経由で画像を取得し、保存する前に (小さいサイズに) サイズ変更しようとしています。保存はできたのですが、サイズ変更ができません。どうすればそれができますか?コードは次のとおりです。

URL url = new URL(LogoURL);

InputStream input = url.openStream();
try {
    OutputStream output = new FileOutputStream("data/data/com.android.mylogo/logo.jpg");
    try {
        //byte[] buffer = new byte[aReasonableSize];     
        int bytesRead = 0;
        System.out.println("Buffer Length is \t:-" + buffer.length);
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
            System.out.println("inside while");
            output.write(buffer, 0, bytesRead);
        }
    } finally {
        output.close();
        System.out.println("saved image");
    }
} finally {
    input.close();
}
4

2 に答える 2

0

画像を特定のサイズに縮小したい場合は、次のコードを使用できます。

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(LogoURL);

try {
    HttpResponse response = client.execute(request);
    final int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        Log.w(LOG_TAG, "Error " + statusCode + " while retrieving bitmap from " + url);
        return null;
    }

    HttpEntity entity = response.getEntity();
    if (entity != null) {

        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = url.openStream();
            bis = new BufferedInputStream(is);

            int sampleSize = 1;

            bis.mark(Integer.MAX_VALUE);

            Options bounds = new Options();
            bounds.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(bis, null, bounds);
            if (bounds.outWidth != -1) {
                int width = bounds.outWidth;
                int height = bounds.outHeight;
                boolean withinBounds = width <= YOUR_DESIRED_WIDTH && height <= YOUR_DESIRED_HEIGHT;

                int newWidth = width;
                int newHeight = height;
                while (!withinBounds) {
                    newWidth /= 2;
                    newHeight /= 2;
                    sampleSize *= 2;
                    withinBounds = newWidth <= YOUR_DESIRED_WIDTH && newHeight <= YOUR_DESIRED_HEIGHT;
                }
            } else {
                Log.w(LOG_TAG, "Can't open bitmap at " + url);
                return null;
            }

            try {
                bis.reset();
            } catch (IOException e) {
                if(is != null){
                    is.close();
                }

                if(bis != null){
                    bis.close();
                }

                if(!entity.isRepeatable()){
                    entity.consumeContent();
                    response = client.execute(request);
                    entity = response.getEntity();
                }

                is = entity.getContent();
                bis = new BufferedInputStream(is);
            }

            Options opts = new Options();
            opts.inSampleSize = sampleSize;
            Bitmap bm = BitmapFactory.decodeStream(bis, null, opts);

            return bm;
        } finally {
            if (is != null) {
                is.close();
            }               

            if (bis != null) {
                bis.close();
            }

            entity.consumeContent();
        }
    }
} catch (IOException e) {
    request.abort();
    Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
    request.abort();
    Log.w(LOG_TAG, "Incorrect URL: " + url);
} catch (Exception e) {
    request.abort();
    Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
}

で画像を開くとOptions bounds = new Options(); bounds.inJustDecodeBounds = true;、画像データはダウンロードされず、画像のサイズのみがダウンロードされます。このサイズを使用して新しいスケール比を計算し、目的の幅と高さを取得します。

このオプションOptions opts = new Options(); opts.inSampleSize = sampleSize;を使用すると、BitmapFactory は既にサイズ変更された画像をダウンロードします。この方法でメモリと帯域幅を節約できます。

sampleSize の値は 2 のべき乗であることに注意してください。異なる数値でも機能しますが、これははるかに効率的です。

于 2012-09-03T09:29:08.277 に答える
0

いずれにしても JPG は圧縮されるため、それ以上圧縮する必要はありません。一般に、何かを圧縮するには、java.util.zipパッケージのクラスを見てください。

于 2012-09-03T09:11:14.513 に答える