自定义 照相机

package com.bingo.activity;

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

import com.bingo.util.Createflie;
import com.bingo.util.ImageCompress;
import com.bingo.util.ImageDispose;
import com.bingo.util.PhotoImageSaveSD;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class BingoPhotoActivity extends Activity {



	//准备一个相机对象
	private Camera mCamera = null;
	//准备一个Bitmap对象
	private Bitmap mBitmap = null;	
	
	private SurfaceView sfv;
	
	private SurfaceHolder  sfh;
	
	ImageButton zpButton,pzButton,fhButton;
	
	Uri originalUri;
	private byte[] mContent;
	private Bitmap myBitmap;
	String imageUrl;
	private ImageView imageView;
	
	LinearLayout ll,ll01 ;
	
	FrameLayout frame;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//窗口去掉标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //窗口设置为全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置窗口为半透明
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
		setContentView(R.layout.photo);

		
		zpButton = (ImageButton) this.findViewById(R.id.xiangpian);
		zpButton.setOnClickListener(new zpButtonL());
		pzButton = (ImageButton) this.findViewById(R.id.paizhao);
		pzButton.setOnClickListener(new pzButtonL());
		fhButton = (ImageButton) this.findViewById(R.id.topfanhui);
		fhButton.setOnClickListener(new fhButtonL());
		
		
		sfv= (SurfaceView) this.findViewById(R.id.SurfaceView01);
		CameraView();

	}

	class zpButtonL implements OnClickListener{
		@Override
		public void onClick(View v) {
//			Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
//			getImage.addCategory(Intent.CATEGORY_OPENABLE);
//			getImage.setType("image/jpeg");
//			startActivityForResult(getImage, 2);
			
			Intent intent = new Intent(BingoPhotoActivity.this,BingoPhotoAlbumActivity.class);
			startActivity(intent);
			
			
			
		}
		
	}
	class pzButtonL implements OnClickListener{
		@Override
		public void onClick(View v) {
			if (mCamera != null) {
			mCamera.takePicture(null, null, pictureCallback);
			}
		}
		
	}
	class fhButtonL implements OnClickListener{
		@Override
		public void onClick(View v) {
			finish();
		}
		
	}
		
	
	public void CameraView() {
		
		Log.i("yao","CameraView");

		// 操作surface的holder
		sfh = sfv.getHolder();
		// 创建SurfaceHolder.Callback对象
		
		//sfh.setFixedSize(100, 150);//设置照相区域大小
		
		sfh.addCallback(new SurfaceHolder.Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				// 停止预览
				mCamera.stopPreview();
				// 释放相机资源并置空
				mCamera.release();
				mCamera = null;
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				//当预览视图创建的时候开启相机
				mCamera = Camera.open();
				try {
					//设置预览
					mCamera.setPreviewDisplay(holder);
				} catch (IOException e) {
					// 释放相机资源并置空
					mCamera.release();
					mCamera = null;
				}

			}

			//当surface视图数据发生变化时,处理预览信息
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

				//获得相机参数对象
				Camera.Parameters parameters = mCamera.getParameters();
				 parameters.set("orientation", "portrait");
				 parameters.set("rotation", "90");

				//设置格式
				parameters.setPictureFormat(PixelFormat.JPEG);
				String string;
				string = parameters.get("picture-size-values");
				Log.i("picture-size01", string);
				String pictureSize[]  = string.split(",");
				String pictureSize01 = pictureSize[2];
				String pictureSize02[] = pictureSize01.split("x");
				String pWidth = pictureSize02[0];
				String pHeight = pictureSize02[1];				
				//设置预览大小,这里我的测试机是Milsstone所以设置的是854x480

				//parameters.setPreviewSize(dm02.widthPixels, dm02.heightPixels);
				//设置自动对焦
				
				//parameters.setFocusMode("auto");
				//设置图片保存时的分辨率大小				
				parameters.setPictureSize(Integer.valueOf(pWidth), Integer.valueOf(pHeight));
				//给相机对象设置刚才设定的参数
				mCamera.setParameters(parameters);
				//开始预览
				mCamera.startPreview();
			}
		});
		// 设置Push缓冲类型,说明surface数据由其他来源提供,而不是用自己的Canvas来绘图,在这里是由摄像头来提供数据
		sfh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	//准备一个保存图片的PictureCallback对象
	public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {

		public void onPictureTaken(byte[] data, Camera camera) {
			Log.i("yao","onPictureTaken");
			//用BitmapFactory.decodeByteArray()方法可以把相机传回的裸数据转换成Bitmap对象
			ImageDispose id = new ImageDispose();
			mBitmap =id.zoomBitmap(BitmapFactory.decodeByteArray(data, 0, data.length), 640, 640);
			//接下来的工作就是把Bitmap保存成一个存储卡中的文件
			String imgurl = null;
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {   
				PhotoImageSaveSD saveSD = new PhotoImageSaveSD();
				imgurl = saveSD.imageSD(mBitmap);
				Intent intent03 = new Intent(BingoPhotoActivity.this,BingoActivityGroup.class);
				intent03.putExtra("imageurl", imgurl);
				intent03.putExtra("id", "1");
				startActivityForResult(intent03, 908);
			}else{
				Intent intent03 = new Intent(BingoPhotoActivity.this,BingoActivityGroup.class);
				intent03.putExtra("imageBitmap", mBitmap);
				intent03.putExtra("id", 2);
				startActivityForResult(intent03, 908);
				//Toast.makeText(BingoPhotoActivity.this, "SDcard不存在",Toast.LENGTH_LONG).show();
				}
			
			

		}

	};
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		ContentResolver resolver = getContentResolver();
		// 获取SD卡中相册的图片
		if (requestCode == 2) {
			try {
				// 获得图片的uri即SD卡路径
				originalUri = data.getData();
				// 将图片内容解析成字节数组
				mContent = readStream(resolver.openInputStream(Uri
						.parse(originalUri.toString())));
				// 将字节数组转换为ImageView可调用的Bitmap对象
				myBitmap = getPicFromBytes(mContent, null);
				// //把得到的图片绑定在控件上显示

				//imageView.setImageBitmap(myBitmap);
				// imageView.setImageURI(originalUri);
				
				
				String[] proj = {MediaStore.Images.Media.DATA};

	            //好像是android多媒体数据库的封装接口,具体的看Android文档
	 
	            Cursor cursor = managedQuery(originalUri, proj, null, null, null); 
	 
	            //按我个人理解 这个是获得用户选择的图片的索引值
	 
	            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
	 
	            //将光标移至开头 ,这个很重要,不小心很容易引起越界
	 
	            cursor.moveToFirst();
	 
	            //最后根据索引值获取图片路径
	 
	            imageUrl = cursor.getString(column_index);

				
			} catch (Exception e) {
				//System.out.println(e.getMessage());
			}
			Intent intent = new Intent(BingoPhotoActivity.this,BingoActivityGroup.class);
				intent.putExtra("imageurl", imageUrl);
				startActivityForResult(intent, 908);
				finish();
		}
	}	
	
	
	/**
	 * @param 将图片内容解析成字节数组
	 * @param inStream
	 * @return byte[]
	 * @throws Exception
	 */
	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;

	}
	/**
	 * @param 将字节数组转换为ImageView可调用的Bitmap对象
	 * @param bytes
	 * @param opts
	 * @return Bitmap
	 */
	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;
	}
	/**
	 * @param 图片缩放
	 * @param bitmap 对象
	 * @param w 要缩放的宽度
	 * @param h 要缩放的高度
	 * @return newBmp 新 Bitmap对象
	 */
	public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		return newBmp;
	}
	
	
	
	
}

	<LinearLayout android:layout_width="fill_parent"
		android:orientation="vertical" android:layout_height="fill_parent"
		android:layout_weight="0.15" android:id="@+id/liner">

		<FrameLayout android:id="@+id/framelayout"
			android:layout_width="fill_parent" android:layout_height="fill_parent">

			<SurfaceView android:id="@+id/SurfaceView01"
				android:layout_width="fill_parent" android:layout_height="fill_parent"></SurfaceView>
		</FrameLayout>
	</LinearLayout>

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值