当前位置: 代码迷 >> Android >> Android之照相
  详细解决方案

Android之照相

热度:60   发布时间:2016-05-01 17:14:54.0
Android之拍照
package cn.itcast.picture;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.PixelFormat;import android.graphics.Bitmap.CompressFormat;import android.hardware.Camera;import android.hardware.Camera.PictureCallback;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.Display;import android.view.KeyEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.Window;import android.view.WindowManager;public class TakeActivity extends Activity {	private static final String TAG = "TakeActivity";    private SurfaceView surfaceView;    private Camera camera;    private boolean preview;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Window window = getWindow();    	requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题    	window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏    	window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//高亮        setContentView(R.layout.main);                surfaceView =(SurfaceView)this.findViewById(R.id.surfaceView);        surfaceView.getHolder().setFixedSize(176, 144);	//设置分辨率        /*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        surfaceView.getHolder().addCallback(new SurfaceCallback());    }        private final class SurfaceCallback implements SurfaceHolder.Callback{		@Override		public void surfaceChanged(SurfaceHolder holder, int format, int width,				int height) {					}		@Override		public void surfaceCreated(SurfaceHolder holder) {			try {				camera = Camera.open();				WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);				Display display = wm.getDefaultDisplay();				Camera.Parameters parameters = camera.getParameters();				parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小				parameters.setPreviewFrameRate(3);//每秒3帧				parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式				parameters.set("jpeg-quality", 85);//照片质量				parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小				camera.setParameters(parameters);				camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面				camera.startPreview();//开始预览				preview = true;			} catch (IOException e) {				Log.e(TAG, e.toString());			}		}		@Override		public void surfaceDestroyed(SurfaceHolder holder) {			if(camera!=null){				if(preview) camera.stopPreview();				camera.release();			}		}    	    }	@Override	public boolean onKeyDown(int keyCode, KeyEvent event) {		if(camera!=null && event.getRepeatCount()==0){			switch (keyCode) {			case KeyEvent.KEYCODE_SEARCH:				camera.autoFocus(null);//自动对焦				break;			case KeyEvent.KEYCODE_CAMERA:			case KeyEvent.KEYCODE_DPAD_CENTER:				camera.takePicture(null, null, new TakePictureCallback());				break;			}		}		return true;	}		private final class TakePictureCallback implements PictureCallback{		@Override		public void onPictureTaken(byte[] data, Camera camera) {			try {				Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);				File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg");				FileOutputStream outStream = new FileOutputStream(file);				bitmap.compress(CompressFormat.JPEG, 100, outStream);				outStream.close();				camera.stopPreview();				camera.startPreview();//开始预览			} catch (Exception e) {				Log.e(TAG, e.toString());			}		}	}    }

?

  <?xml version="1.0" encoding="utf-8" ?> - <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.picture" android:versionCode="1" android:versionName="1.0">- <application android:icon="@drawable/icon" android:label="@string/app_name">- <activity android:name=".TakeActivity" android:label="@string/app_name" android:screenOrientation="landscape">- <intent-filter>  <action android:name="android.intent.action.MAIN" />   <category android:name="android.intent.category.LAUNCHER" />   </intent-filter>  </activity>  </application>  <uses-sdk android:minSdkVersion="7" />   <uses-permission android:name="android.permission.CAMERA" /> - <!--  在SDCard中创建与删除文件权限   -->   <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> - <!--  往SDCard写入数据权限   -->   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   </manifest>

?

1 楼 freedomray 2011-09-23  
楼主您好。
使用了你方法存储拍照的图片。但是为什么在手机上不能预览呢?拷到电脑上是可以预览的。手机型号是G3 Hero。

不知楼主是否也遇到这个问题。
2 楼 haojunming11 2011-10-27  
我现在是用htc g8做测试,进行相机拍照以后就让界面停留在刚刚拍摄的图片上,然后点击预览进入预览界面,然后再次拍照都没有问题,可是我给相机设置焦距,分辨率,或者曝光度以后在拍照系统就会卡住,过一会就挂掉了,同时其它拍照软件也不能用,估计应该是Fail to connect to camera service是我调用camera。release方法不成功吗?还是另有其它原因。
  相关解决方案