Android4.1 无预览拍照

1、setParameters failed解决方法

2、无预览拍照


客户的需求千奇百怪,有要静默安装功能的,下载编译好的ROM,在ROM中加入自己的应用程序实现静默安装,为了盈利好无耻啊。现在要实现无预览拍照功能,哥们又哭笑不得,客户是上帝,木有办法。据说无预览拍照在4.1中无法实现了。因为牵涉到用户隐私问题。

看代码吧,其实实现特简单。主要是思路问题。

public class NoPreviewTakePicActivity extends Activity{
	private Camera mCamera;
	private CameraPreview mPreview;
	private ToneGenerator mTone;
	private static final int OPTION_SNAPSHOT = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPreview = new CameraPreview(this);
		setContentView(R.layout.main);
		addContentView(mPreview, new LayoutParams(1, 1));// 在Layout中加入了一个宽高为1的一个预览区域。但用户确实看不到,就莫名其妙的拍照了。
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		switch(itemId){
		case OPTION_SNAPSHOT:
			mCamera.takePicture(shutterCallback, null, jpegCallback);
			break;
		}
		return true;
	}
	private PictureCallback jpegCallback = new PictureCallback(){

		public void onPictureTaken(byte[] data, Camera camera) {
			Parameters ps = camera.getParameters();
			if(ps.getPictureFormat() == PixelFormat.JPEG){
			    String path = save(data);
			    Uri uri = Uri.fromFile(new File(path));
   			    Intent intent = new Intent();
   			    intent.setAction("android.intent.action.VIEW");
   			    intent.setDataAndType(uri, "image/jpeg");
   			    startActivity(intent);
			}
		}
	};
	
	private ShutterCallback shutterCallback = new ShutterCallback(){
		public void onShutter() {
			if(mTone == null){
				mTone = new ToneGenerator(AudioManager.STREAM_MUSIC,ToneGenerator.MAX_VOLUME);
			}
			mTone.startTone(ToneGenerator.TONE_PROP_BEEP2);
		}
	};
	
	private String save(byte[] data){
		String path = "/mnt/sdcard/"+System.currentTimeMillis()+".jpg";
		try {
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
				String storage = Environment.getExternalStorageDirectory().toString();
				StatFs fs = new StatFs(storage);
				long available = fs.getAvailableBlocks()*fs.getBlockSize();
				if(available<data.length){
					return null;
				}
				File file = new File(path);
				if(!file.exists()){
					file.createNewFile();
				}
				FileOutputStream fos = new FileOutputStream(file);
				fos.write(data);
				fos.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return path;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, OPTION_SNAPSHOT, 0, R.string.snapshot);
		return super.onCreateOptionsMenu(menu);
	}

	class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

		SurfaceHolder mHolder;

		public CameraPreview(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);
			//mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // 4.0+ auto
		}
		public void surfaceCreated(SurfaceHolder holder) {
			mCamera = Camera.open(1);
			try {
				mCamera.setPreviewDisplay(holder);
			} catch (IOException exception) {
				mCamera.release();
				mCamera = null;
			}
		}

		public void surfaceDestroyed(SurfaceHolder holder) {
			mCamera.stopPreview();
			mCamera.release();
			mCamera = null;
		}

		public void surfaceChanged(SurfaceHolder holder, int format, int w,
				int h) {
			Camera.Parameters parameters = mCamera.getParameters();
			Size size = parameters.getPreviewSize();
			Log.e(VIEW_LOG_TAG, "size.width="+size.width+"size.height="+size.height);
			// setPreviewSize 宽高不能大于 parameters.getPreviewSize(),否者会出现java.lang.RuntimeException: setParameters failed错误
			//parameters.setPreviewSize(800, 600);
			mCamera.setParameters(parameters);
			mCamera.startPreview();
		}
		
	}
}

为了防止一些侵犯用户隐私和一些非法吸费等软件,安装应用程序时千万注意看应用的权限;喜欢折腾刷机的朋友尽量从官网下载ROM。应用程序尽量从Google Play下载。

原创文章转载请注明出处:http://blog.csdn.net/tangcheng_ok

你可能感兴趣的:(android,android,android,android,Camera,setPa,无预览拍照)