android 图片压缩

NativeUtil.java

 

 

package net.bither.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

public class NativeUtil {
 private static int DEFAULT_QUALITY = 95;
 private static int COMPRESS_COUNT=10;//压缩倍数

 public static void compressBitmap(Bitmap bit, String fileName,
   boolean optimize) {
  compressBitmap(bit, DEFAULT_QUALITY, fileName, optimize);

 }

 public static void compressBitmap(Bitmap bit, int quality, String fileName,
   boolean optimize) {
  Log.d("native", "compress of native");

  // if (bit.getConfig() != Config.ARGB_8888) {
  Bitmap result = null;

  result = Bitmap.createBitmap(bit.getWidth() / COMPRESS_COUNT, bit.getHeight() / COMPRESS_COUNT,
    Config.ARGB_8888);// 缂╁皬3鍊�
  Canvas canvas = new Canvas(result);
  Rect rect = new Rect(0, 0, bit.getWidth(), bit.getHeight());// original
  rect = new Rect(0, 0, bit.getWidth() / COMPRESS_COUNT, bit.getHeight() / COMPRESS_COUNT);// 缂╁皬3鍊�
  canvas.drawBitmap(bit, null, rect, null);
  saveBitmap(result, quality, fileName, optimize);
  result.recycle();
  // } else {
  // saveBitmap(bit, quality, fileName, optimize);
  // }

 }

 private static void saveBitmap(Bitmap bit, int quality, String fileName,
   boolean optimize) {

  compressBitmap(bit, bit.getWidth(), bit.getHeight(), quality,
    fileName.getBytes(), optimize);

 }

 private static native String compressBitmap(Bitmap bit, int w, int h,
   int quality, byte[] fileNameBytes, boolean optimize);

 static {
  System.loadLibrary("jpegbither");
  System.loadLibrary("bitherjni");

 }

}

 

package com.example.testjpg;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import net.bither.util.NativeUtil;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;


public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  testJpeg();
 }

 private void testJpeg() {
  new Thread(new Runnable() {
   public void run() {
    try {
     int quality = 90;//original
     quality=50;
     InputStream in = getResources().getAssets()
       .open("test2.jpg");
     Bitmap bit = BitmapFactory.decodeStream(in);
     //File dirFile = getExternalCacheDir();
     String curentDirectory = Environment.getExternalStorageDirectory()
       .getAbsolutePath() + "/test/";
     File dirFile = new File(curentDirectory);
     if (!dirFile.exists()) {
      dirFile.mkdirs();
     }
     File originalFile = new File(dirFile, "original.jpg");
     FileOutputStream fileOutputStream = new FileOutputStream(
       originalFile);
     bit.compress(CompressFormat.JPEG, quality, fileOutputStream);
     File jpegTrueFile = new File(dirFile, "jpegtrue.jpg");
     File jpegFalseFile = new File(dirFile, "jpegfalse.jpg");
     NativeUtil.compressBitmap(bit, quality,
       jpegTrueFile.getAbsolutePath(), true);
     NativeUtil.compressBitmap(bit, quality,
       jpegFalseFile.getAbsolutePath(), false);
//     Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

   }
  }).start();
 }
}


//jni压缩文件

你可能感兴趣的:(android 图片压缩)