Compress Pictures of summary

 

 

1.Android the picture compression method

===

 

 1.Android the picture compression method

Bitmap

  1. Android is present in the picture Bitmap (bitmap) form, common bitmap file formats: .bmp .jpg .png .gif.
  2. Bitmap image size calculated length = width of the image the number of bytes occupied by the unit pixel.
  3. Bitmap advantages and disadvantages
  • Advantages: rich color change, may change the shape of any region of color display.
  • Disadvantages: Zoom in and out and out will cause an increase in pixels, so images will appear distorted or jagged. Another disadvantage is that the higher the amount of memory larger pixel data occupies greater.

Common compression methods

Quality compression
  1. Quality compression will not change the picture width and height (pixels), it is to change the bit depth and clarity of the picture.
  2. png lossless compression, so the quality of compression is ineffective png
/**
     * 质量压缩
     * 降低图片的质量,不会减少图片的像素,改变图片的位深和透明度,没有改变像素大小,所以不会减少占据的内存大小。
     */
    private void qualityCompress(Bitmap.CompressFormat format, int quality, String type) { try { File file = new File(Environment.getExternalStorageDirectory(), "test_" + quality + format + "_" + type + ".jpg"); //得到一个文件输入流 mFileOutputStream = new FileOutputStream(file); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_pic); // 图片的格式 quality是压缩完之后保存图片设置的质量参数 png是无损压缩,quality参数对png无效 bitmap.compress(format, quality, mFileOutputStream); } catch (FileNotFoundException exception) { exception.printStackTrace(); } finally { if (mFileOutputStream != null) { try { mFileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } 
Sampling rate reduction
  1. Reduce pixel image. The higher the value, the lower the image pixels.
/**
     * 尺寸压缩中的采样率压缩
     * 改变了像素,减少了图片占用的内存,同样照片的清晰度也降低了。
     * inSampleSize = 2 内存将减少 1/4  内存大小 = 图片宽/inSampleSize * 图片高/inSampleSize * 单位像素占用的字节大小
     */
    private void loadImage() { BitmapFactory.Options options = new BitmapFactory.Options(); //设置为true 并会将图片加载到内存中,但是可以获取到图片的宽和高 通过options options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.drawable.ic_pic, options); int outWidth = options.outWidth; int outHeight = options.outHeight; Log.d(TAG, "loadImage: width = " + outWidth); Log.d(TAG, "loadImage: height = " + outHeight); //这个地方根据获取到大小和想要显示的大小做缩放 options.inSampleSize = calculateInSampleSize(options, 200, 200); Log.d(TAG, "loadImage: inSampleSize = " + options.inSampleSize); //设置为false 这回再去解码图片可以将其读取到内存中了。 options.inJustDecodeBounds = false; mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_pic, options); File file = new File(Environment.getExternalStorageDirectory(), "text_" + "just_100_no_inSampleSize.jpg"); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // mImageView.setImageBitmap(mBitmap); } 

That is: If set to true, then inJustDecoedBounds, decoding bitmap can return only its height, width and Mime type, without its application memory, thus saving memory space.
Options = new new BitmapFactory.Options BitmapFactory.Options ();
options.inJustDecodeBounds = to true;
BitmapFactory.decodeResource (getResources (), R.id.myimage, Options);
int = imageHeight options.outHeight;
int = imageWidth options.outWidth;
String imageType = options.outMimeType;

 /** * 计算图片的缩放比例 * * @param options * @param reqHeight * @param reqWidth */ private int calculateInSampleSize(BitmapFactory.Options options, int reqHeight, int reqWidth) { int height = options.outHeight; int width = options.outWidth; //缩放的比例 int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { int halfHeight = height / 2; int halfWidth = width / 2; while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; } 
Scaling compression method
  1. Reduced pixel images generated picture file size is also smaller.
/**
     * 尺寸压缩中的 按缩放比压缩 这里的缩放比是事先定义好的 和采样率相比 采样率是根据给定的预期大小去计算缩放比
     *
     * @param mBitmap
     * @param mFile
     */
    private void compressBitmapToFile(Bitmap mBitmap, File mFile) { //设置压缩倍数 int ratio = 2; //压缩Bitmap到对应的尺寸 压缩格式 ARGB_8888 4字节 一个像素需要4个字节来存储 Bitmap resultBitmap = Bitmap.createBitmap(mBitmap.getWidth() / ratio, mBitmap.getHeight() / ratio, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(resultBitmap); Rect rect = new Rect(0, 0, mBitmap.getWidth() / ratio, mBitmap.getHeight() / ratio); canvas.drawBitmap(mBitmap, null, rect, null); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); File file = new File(Environment.getExternalStorageDirectory(), "test_compress_100.jpg"); resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); try { mFileOutputStream1 = new FileOutputStream(file); mFileOutputStream1.write(byteArrayOutputStream.toByteArray()); mFileOutputStream1.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (mFileOutputStream1 != null) { try { mFileOutputStream1.close(); } catch (IOException e) { e.printStackTrace(); } } } } 

Guess you like

Origin www.cnblogs.com/awkflf11/p/12611801.html