Android Camera拍照 压缩
http://www.linuxidc.com/Linux/2014-12/110924.htm
package com.klp.demo_025; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private Button button; private File file; private Uri uri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); button = (Button) findViewById(R.id.button1); file = new File(this.getExternalFilesDir(null), "image.jpg"); uri = Uri.fromFile(file); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, 2); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == 2) { startPhotoZoom(uri); } else { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), options); // 压缩图片 // bitmap = compressImage(bitmap,500); if (bitmap != null) { // 显示图片 iv.setImageBitmap(bitmap); // 保存图片 FileOutputStream fos = null; fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } } catch (Exception e) { // TODO: handle exception } } } } /** * 裁剪图片 * * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面. intent.putExtra("aspectX", 1);// 这两项为裁剪框的比例. intent.putExtra("aspectY", 1);// x:y=1:1 intent.putExtra("outputX", 200);//图片输出大小 intent.putExtra("outputY", 200); intent.putExtra("output", uri); intent.putExtra("outputFormat", "JPEG");// 返回格式 startActivityForResult(intent, 3); } /** * 将图片image压缩成大小为 size的图片(size表示图片大小,单位是KB) * * @param image * 图片资源 * @param size * 图片大小 * @return Bitmap */ private Bitmap compressImage(Bitmap image, int size) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 while (baos.toByteArray().length / 1024 > size) { // 重置baos即清空baos baos.reset(); // 每次都减少10 options -= 10; // 这里压缩options%,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, options, baos); } // 把压缩后的数据baos存放到ByteArrayInputStream中 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把ByteArrayInputStream数据生成图片 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; } }
package com.carloz.invokecamera; import android.content.ContentValues; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.SystemClock; import android.provider.MediaStore; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; public class InvokeCameraActivity extends ActionBarActivity { private String TAG = "CARLOZ"; private ImageView iv; private Button button; private File file; private Uri uri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_invoke_camera); iv = (ImageView) findViewById(R.id.imageView1); button = (Button) findViewById(R.id.button1); file = new File("/data/sdcard/DCIM/Camera/carloz.jpg"); uri = Uri.fromFile(file); // String imageName = "carlo" + System.currentTimeMillis(); // ContentValues values = new ContentValues(); // values.put(MediaStore.Images.Media.TITLE, imageName); // values.put(MediaStore.Images.Media.DISPLAY_NAME, imageName+".ipg"); // values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg"); // uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Log.d(TAG, "onCreate - uri: " + uri); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); //intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, 3); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, "onActivityResult requestCode=" + requestCode + ", resultCode=" + resultCode + ", data=" + data); if (resultCode == RESULT_OK) { switch (requestCode) { case 1: iv.setImageURI(uri); break; case 2: startPhotoZoom(uri); break; case 3: try { Log.d(TAG, "onActivityResult BitmapFactory filepath: " + file.getAbsolutePath()); //BitmapFactory.Options options = new BitmapFactory.Options(); // options.inSampleSize = 2; InputStream is = new FileInputStream(file.getAbsolutePath());// 读出指定的文件,二进制流 Bitmap bitmap = BitmapFactory.decodeStream(is); //Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //bitmap = compressImage(bitmap,500); if (bitmap != null) { Log.d(TAG, "onActivityResult bitmap != null"); iv.setImageBitmap(bitmap); }else{ Log.d(TAG, "onActivityResult - bitmap is null"); } is.close(); } catch (Exception e) { // TODO: handle exception Log.d(TAG, "onActivityResult - BitmapFactory Exception: " + e.toString()); } break; default: Log.d(TAG, "onActivityResult - request code not define !"); break; } }else{ Log.d(TAG, "onActivityResult - RESULT NOT OK"); } super.onActivityResult(requestCode, resultCode, data); } /** * 裁剪图片 * * @param uri */ public void startPhotoZoom(Uri uri) { Log.d(TAG, "startPhotoZoom - uri: " + uri.toString()); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面. intent.putExtra("aspectX", 1);// 这两项为裁剪框的比例. intent.putExtra("aspectY", 1);// x:y=1:1 intent.putExtra("outputX", 200);//图片输出大小 intent.putExtra("outputY", 200); intent.putExtra("output", uri); intent.putExtra("outputFormat", "jpg");// 返回格式 startActivityForResult(intent, 3); } /** * 将图片image压缩成大小为 size的图片(size表示图片大小,单位是KB) * * @param image 图片资源 * @param size 图片大小 * @return Bitmap */ private Bitmap compressImage(Bitmap image, int size) { Log.d(TAG, "compressImage - size: " + size); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 while (baos.toByteArray().length / 1024 > size) { // 重置baos即清空baos baos.reset(); // 每次都减少10 options -= 10; // 这里压缩options%,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, options, baos); } // 把压缩后的数据baos存放到ByteArrayInputStream中 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把ByteArrayInputStream数据生成图片 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; } }