Android的 - 以图片而不preVIEW [英] Android - Take picture without preview
本文介绍了Android的 - 以图片而不preVIEW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我的应用程序开始运行时拍摄照片时不preVIEW,立即后保存在新文件夹中的图片 - pictures123,在根文件夹。
可能有人请告诉我,什么是错在我的code?
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
文件目录=新的文件(Environment.getExternalStorageDirectory()+/ pictures123 /); 如果(!directory.exists()){
directory.mkdir();
} 摄像头摄像头= Camera.open(0);
Camera.Parameters参数= camera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
camera.setParameters(参数);
SurfaceView MVIEW =新SurfaceView(getBaseContext());
camera.set previewDisplay(mview.getHolder());
camera.set previewDisplay(NULL);
camera.start preVIEW();
camera.takePicture(NULL,NULL,photoCallback);
camera.stop preVIEW();
}Camera.PictureCallback photoCallback =新Camera.PictureCallback(){
公共无效onPictureTaken(字节[]数据,相机摄像头){ 尝试{
。字符串根= Environment.getExternalStorageDirectory()的toString();
文件MYDIR =新的文件(根+/ pictures123);
档案文件=新的文件(MYDIRpic1.jpeg);
FileOutputStream中出=新的FileOutputStream(文件);
out.write(数据);
了out.flush();
out.close();
}赶上(FileNotFoundException异常五){
e.printStackTrace(); }赶上(IOException异常五){
e.printStackTrace(); }赶上(例外五)
{
e.printStackTrace();
}
完(); }
};
权限:
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
<使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAGE/>
<使用许可权的android:NAME =android.permission.CAMERA/>
<使用特征的android:NAME =android.hardware.camera/>
解决方案
您不能把没有preVIEW图片,但你不必在屏幕上显示preVIEW。您可以直接输出到而不是表面纹理(API 11 +)。
请参阅this回答了解更多详情。
I am trying to take a picture without preview, immediately when my application starts running and after that to save the picture in new folder - "pictures123", in the root folder. Could someone please tell me what's wrong in my code?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File directory = new File(Environment.getExternalStorageDirectory() + "/pictures123/");
if (!directory.exists()) {
directory.mkdir();
}
Camera camera = Camera.open(0);
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
camera.setParameters(parameters);
SurfaceView mview = new SurfaceView(getBaseContext());
camera.setPreviewDisplay(mview.getHolder());
camera.setPreviewDisplay(null);
camera.startPreview();
camera.takePicture(null,null,photoCallback);
camera.stopPreview();
}
Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/pictures123");
File file = new File (myDir, "pic1.jpeg");
FileOutputStream out = new FileOutputStream(file);
out.write(data);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
finish();
}
};
permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
解决方案
You can't take a picture without a preview, but you don't have to show the preview on screen. You can direct the output to a SurfaceTexture instead (API 11+).
See this answer for more details.
这篇关于Android的 - 以图片而不preVIEW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文