마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

이미지 용량 줄이는 방법 문의합니다.

0 추천
안녕하세요. 이미지를 서버로 전송하는 프로그램을 구현하면서 풀지 못한 문제가 있어 문의합니다.

 

서버에 이미지를 전송하고 받아오고 하기 위해서

BitmapFatory.Options 의 inSampleSize 이용해서 사진의 크기를 줄였습니다.

여기서 궁금한건 이미지 크기는 그대로 두고 화질을 조금 떨어뜨려서 용량을더 줄이고 싶습니다.

 

크기를 줄이는방법은 많이 나오는데 화질을 줄이는 방법은 모르겠더라구요.

 방법을 아시는 분 도움 주시면 감사하겠습니다.
익명사용자 님이 2014년 7월 4일 질문

2개의 답변

0 추천
private static final int PREVIEW_QUALITY = 80;
...
Bitmap bitmap ...

ByteArrayOutputStream out = new ByteArrayOutputStream();
Rect area = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
bitmap .compressToJpeg(area, PREVIEW_QUALITY, out);
byte[] sendData = out.toByteArray();

 

카라드레스 (2,910 포인트) 님이 2014년 7월 4일 답변
감사합니다!! 도움 많이 됐습니다.
0 추천
 
BitmapFatory.Options opts = new BitmapFatory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);
//여기 두번째 파라미터가 품질을 0~100까지 조절합니다
bitmap.compress(format, quality, stream);
 
아래는 compress 메서드의 설명입니다.
 
/**
     * Write a compressed version of the bitmap to the specified outputstream.
     * If this returns true, the bitmap can be reconstructed by passing a
     * corresponding inputstream to BitmapFactory.decodeStream(). Note: not
     * all Formats support all bitmap configs directly, so it is possible that
     * the returned bitmap from BitmapFactory could be in a different bitdepth,
     * and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque
     * pixels).
     *
     * @param format   The format of the compressed image
     * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
     *                 small size, 100 meaning compress for max quality. Some
     *                 formats, like PNG which is lossless, will ignore the
     *                 quality setting
     * @param stream   The outputstream to write the compressed data.
     * @return true if successfully compressed to the specified stream.
     */
    public boolean compress(CompressFormat format, int quality, OutputStream stream) {
        checkRecycled("Can't compress a recycled bitmap");
        // do explicit check before calling the native method
        if (stream == null) {
            throw new NullPointerException();
        }
        if (quality < 0 || quality > 100) {
            throw new IllegalArgumentException("quality must be 0..100");
        }
        return nativeCompress(mNativeBitmap, format.nativeInt, quality,
                              stream, new byte[WORKING_COMPRESS_STORAGE]);
    }

 

whdrb19 (23,520 포인트) 님이 2014년 7월 4일 답변
감사합니다!! 도움 많이 됐습니다.
...