Android Camera 无法拍照

在常规的拍照方法在特殊的样机上异常时(有预览,take picture无异常信息,无回掉,甚至各大拍照软件也无法通过take picture 拍照时)找了一种折中的方式。通过

camera 的 PreviewCallback  回调的每一帧的数据,保存为image。主要问题是 会调过来的byte[] 格式为YUV420,需要一步转化。这样拍出的照片会比较小,因为是预览帧过来的。实属无奈。

@Override
			public void onPreviewFrame(byte[] data, Camera camera) {
				// TODO Auto-generated method stub
				if (isFocus) {
					isFocus = false;
					try {
						Camera.Parameters parameters = camera.getParameters();
						int width = parameters.getPreviewSize().width;
						int height = parameters.getPreviewSize().height;
						YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);

						ByteArrayOutputStream out = new ByteArrayOutputStream();
						yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);

						byte[] bytes = out.toByteArray();

						String strPath = Environment.getExternalStorageDirectory().toString() + "/Test/photos/";
						File ff = new File(strPath);
						if (!ff.exists()) {
							ff.mkdirs();
						}
						String fileName = System.currentTimeMillis() + ".jpg";
						FileOutputStream bos = new FileOutputStream(new File(strPath + fileName));
						bos.write(bytes);
						bos.flush();
						bos.close();
						handler.obtainMessage(MSG_GET_PIC, strPath + fileName).sendToTarget();
					} catch (Exception e) {
						System.out.println(e.getMessage());
					}
				}
			}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin4ch

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值