【发布时间】:2013-07-19 15:31:44
【问题描述】:
我有几天试图在 android 中捕捉照片,想法是在捕捉图像后将文件上传到 FTP。我有一个扩展SurfaceView的类,当它变得可见时,播放设备后置摄像头的图像,就在这里。单击相机的图像,我想将图像保留为 JPEG 格式。
我已经尝试了几十种解决方案,但我现在得到的是一个全黑的 JPEG。我认为我的方式是正确的,但缺少一些东西。
这是我在自定义类的 onClick 事件中使用的代码:
try {
//create bitmap screen capture
Bitmap bitmap;
this.setDrawingCacheEnabled(true);
//Freezes the image
mCamera.stopPreview();
bitmap = Bitmap.createBitmap(getDrawingCache(), 0, 0, this.getLayoutParams().width, this.getLayoutParams().height);
this.setDrawingCacheEnabled(false);
//Create temp file
file = File.createTempFile("prueba", ".JPG", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
//Copy bitmap content into file
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 60, fos);
fos.flush();
fos.close();
//Free camera
mCamera.release();
mCamera = null;
}catch(Exception e){
e.printStackTrace();
}
mCamera 是 android.hardware.Camera,选择了特定的大小并将宽度和高度复制到 this.layoutParams 以很好地适应,这一切都在 surfaceCreated() 方法中。
希望有人可以帮助我解决任何问题。
非常感谢。
【问题讨论】:
标签: android file bitmap camera surfaceview