APP--调用照相机拍照取得图片and从相册取得图片

package com.camera.fxft;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CameraDemo extends Activity {
/** Called when the activity is first created. */
ImageView img;
Bitmap myBitmap;
byte[] mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btCamera = (Button)findViewById(R.id.btCamera);
Button btSelect = (Button)findViewById(R.id.btSelect);
img = (ImageView)findViewById(R.id.img);
btCamera.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
openCamera();
}

});
btSelect.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
openPhotos();
}

});

}



public void openPhotos()
{
Intent getImg = new Intent(Intent.ACTION_GET_CONTENT);
getImg.addCategory(Intent.CATEGORY_OPENABLE);
getImg.setType("image/*");
//可截图
// getImg.putExtra("crop", "true");
// getImg.putExtra("aspectX", 1);
// getImg.putExtra("aspectY", 1);
// getImg.putExtra("outputX", 352);
// getImg.putExtra("outputY", 240);
// getImg.putExtra("return-data", true);
startActivityForResult(getImg, 0);
}

public void openCamera()
{
Intent getImgByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImgByCamera, 1);
}

public static byte[] readStream ( InputStream inStream ) throws Exception
{
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1)
{
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;

}

public static Bitmap getPicFromBytes ( byte[] bytes , BitmapFactory.Options opts )
{
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
ContentResolver resolver = getContentResolver();
Log.d("carera", "-requestCode----------===="+requestCode);
if (requestCode == 0) {
try {
//返回原图
Uri imgUri = data.getData();
mContent = readStream(resolver.openInputStream(Uri.parse(imgUri.toString())));
myBitmap = getPicFromBytes(mContent, null);
img.setImageBitmap(myBitmap);
//返回截图
// myBitmap = (Bitmap) data.getExtras().get("data");
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// baos.toByteArray();
// img.setImageBitmap(myBitmap);
} catch (Exception e) {
// TODO: handle exception
Log.d("carera", "--------error---"+e.toString());
}
}else if (requestCode == 1) {
try {
Bundle extras = data.getExtras();
myBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.toByteArray();
img.setImageBitmap(myBitmap);
} catch (Exception e) {
// TODO: handle exception
}
}
}


}

posted @ 2011-11-15 10:36  厘米  阅读(510)  评论(0编辑  收藏  举报