引用:http://blog.sina.com.cn/s/blog_821e2bb10101475x.html
第一种:
字节数组data保存Bitmap对象转为字节数组,处理代码:
BitmapFactory.decodeByteArray(data, 0, data.length);
第二种:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data2 = baos.toByteArray();
这里其实很好理解,第二种方法使用了Bitmap的compress方法,一般用于保存一个Bitmap对象,转为字节输出流,但是compress目前编码由两种,比如JPG,一般处理照片和PNG,PNG一般处理带Alpha透明通道的图片,后面的第二个参数是清晰度,一般100是最高,0是最低,这个值越大图片越清晰,同时文件体积越大,JPG和PNG都是压缩的图片,所以和原始的直接通过 BitmapFactory.decodeByteArray解码后的大小会有很大的不同。
android调用照相机拍照获取照片并做简单剪裁:
1.调用系统的照相机程序 Intent intent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,TAKE_PHOTO_WITH_DATA);
2.在onActivityResult中处理返回的data final Bitmap photo = data.getParcelableExtra("data"); 如果不需要做剪切处理就可以直接使用图片了,比如输出到ImageView上 imageView.setImageBitmap(photo); 如果需要做剪切处理,就继续向下执行
3.做剪切处理 Intent intent = newIntent("com.android.camera.action.CROP"); intent.setType("image private Button btnTake = null; private ImageView lblImage = null; private static final int PHOTO_PICKED_WITH_DATA = 3021; private static final int CAMERA_WITH_DATA = 3023; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnTake = (Button)findViewById(R.id.btnTake); lblImage = (ImageView)findViewById(R.id.lblImage); btnTake.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, CAMERA_WITH_DATA); } }); } @Override protected void onActivityResult(int requestCode, int resultCode,Intent data) { // TODO Auto-generated method stub if(resultCode!=RESULT_OK) return; switch(requestCode){ case CAMERA_WITH_DATA: final Bitmap photo = data.getParcelableExtra("data"); if(photo!=null){ doCropPhoto(photo); } case PHOTO_PICKED_WITH_DATA: Bitmap photo1 = data.getParcelableExtra("data"); if(photo1!=null){ lblImage.setImageBitmap(photo1); } } } protected void doCropPhoto(Bitmap data){ Intent intent = getCropImageIntent(data); startActivityForResult(intent, PHOTO_PICKED_WITH_DATA); } public static Intent getCropImageIntent(Bitmap data) { Intent intent = newIntent("com.android.camera.action.CROP"); intent.setType("image/*"); intent.putExtra("data", data); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 128); intent.putExtra("outputY", 128); intent.putExtra("return-data", true); return intent; } }
完整代码:
package com.chc.android;
import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView;
public class tapgetcut extends Activity { private Button btnTake = null; private ImageView lblImage = null;
private static final int PHOTO_PICKED_WITH_DATA = 3021; private static final int CAMERA_WITH_DATA = 3023;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnTake = (Button)findViewById(R.id.btn01); lblImage = (ImageView)findViewById(R.id.iv01); btnTake.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, CAMERA_WITH_DATA); } }); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if(resultCode!=RESULT_OK) return; switch(requestCode){ case CAMERA_WITH_DATA: final Bitmap photo = data.getParcelableExtra("data"); if(photo!=null){ doCropPhoto(photo); } case PHOTO_PICKED_WITH_DATA: Bitmap photo1 = data.getParcelableExtra("data"); if(photo1!=null){ lblImage.setImageBitmap(photo1); } } }
protected void doCropPhoto(Bitmap data){ Intent intent = getCropImageIntent(data); startActivityForResult(intent, PHOTO_PICKED_WITH_DATA); }
public static Intent getCropImageIntent(Bitmap data) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setType("image/*"); intent.putExtra("data", data); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 128); intent.putExtra("outputY", 128); intent.putExtra("return-data", true); return intent; } }
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id ="@+id/test01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/btn01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="确定" /> <ImageView android:id="@+id/iv01" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ImageView> </LinearLayout>
|
注:貌似这篇文章与题目不符,仔细看看这两个模块,是不是豁然开朗呢