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;
}
}
Android Camera拍照 压缩的更多相关文章
- Android Camera 拍照 三星BUG总结
Android Camera 三星BUG : 近期在Android项目中使用拍照功能 , 其他型号的手机执行成功了 只有在三星的相机上遇到了bug . BUG详细体现为 : (1) 摄像头拍照后图 ...
- android Camera拍照 及 MediaRecorder录像 预览图像差90度
Camera拍照: 今天做照相机程序,结果写好了发现出问题了,预览的图像差90度.相关源代码如下: Camera.Parameters params = camera.getParameters(); ...
- [置顶] android系统如何在静音模式下关闭camera拍照声音(2)
之前写过一篇“android系统如何在静音模式下关闭camera拍照声音”的博客,今天来写他的续篇,继续探讨这个问题. 公司新需求,要求在camera应用中添加一个开关,可以进行拍照声音的关闭和开启. ...
- Android开发技巧——Camera拍照功能
本篇是我对开发项目的拍照功能过程中,对Camera拍照使用的总结.由于camera2是在api level 21(5.0.1)才引入的,而Camera到6.0仍可使用,所以暂未考虑camera2. 文 ...
- Android Camera开发系列(下)——自定义Camera实现拍照查看图片等功能
Android Camera开发系列(下)--自定义Camera实现拍照查看图片等功能 Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 上 ...
- Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...
- Android 修改Camera拍照的默认保存路径
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 浅谈Android中拍照、从相册选择图片并截图相关知识点
前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...
- Android Camera 使用小结
Android手机关于Camera的使用,一是拍照,二是摄像,由于Android提供了强大的组件功能,为此对于在Android手机系统上进行Camera的开发,我们可以使用两类方法:一是借助Inten ...
随机推荐
- DrawerLayout和toolbar的使用
onPostCreate()是Activity完全启动后的调用:在完全启动后的回调设置toolbar 然后在使用 AppCompatActivity 时style要设置为何appCompat相关的样式 ...
- JAVA 面向对象-2-继承(Inheritance)
i.继承(Inheritance) 1.继承的概念 继承:在面向对象编程的过程中,通过扩展一个已有的类,并继承该类的属性和行为,来创建一个新的类. 继承是面向对象编程最重要的特征之一. 继承的优点:1 ...
- final、抽象类、接口、多态、
final———最终.作为一个修饰符 可以修饰类. 函数. 变量: 被final修饰的类不可以被继承: 被final修饰的方法不可以被重写: 被final修饰的变量只能够被赋值一次,既可以修饰成 ...
- Java整型与字符串相互转换(转)
1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([S ...
- 在java项目中应用ueditor
虽然百度ueditor的官网和文档都已经很详细了.但是自己还是记录下 自己使用uEditor的过程. 这是 他的官网 http://ueditor.baidu.com/website/ 例子 文档什 ...
- Java appendReplacement 和 appendTail 方法
Matcher 类也提供了appendReplacement 和appendTail 方法用于文本替换: 看以下的样例来解释这个功能: import java.util.regex.Matcher; ...
- MFC ListControl使用方法
在原来博客中有:MF CListControl 简单功能使用 推荐文章:MFC类CtrlList用法 今天又又一次来介绍点新东西:双击击listcontrol 做出响应.当然你能够做的还有非常多,比 ...
- hdu1864 最大报销额(01背包)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1864 Problem ...
- ExecutorService(转)
ExecutorService 建立多线程的步骤: 1.定义线程类 class Handler implements Runnable{ } 2.建立ExecutorService线程池 Execut ...
- 一个基于MVVM的TableView组件化实现方案
AITableView https://github.com/chentoo/AITableView cocoapods: pod ‘AITableView’ 做什么用? 这是一个简化UITableV ...