gpt4 book ai didi

android - OnPictureTaken() 计时/保存问题

转载 作者:行者123 更新时间:2023-11-29 17:53:21 28 4
gpt4 key购买 nike

我正在创建一个自定义相机应用程序(它将包含在一个更大的主应用程序中),它使用相机加载 View - 预览和图片库预览 - 因此用户可以拍照或选择一个预先存在的。如果用户确实拍照,我只想显示一个确认对话框,如果确认,则将控制权交还给主要 Activity 。我无法存储照片:需要快速将其用作附件,并在通过主要 Activity 发送后将其处理掉。

当我拍照并调用 onPictureTaken() 时出现问题:它要么不起作用,要么需要很长时间才能保存照片。另外,我似乎无法弄清楚如何在不将照片保存到目录的情况下当场提取照片;我计划立即删除该目录,但必须有一种更简洁的方法(本质上,我只想访问原始字节数组,而不必保存它)。

我希望它是模块化的(我将在即将进行的项目中大量使用相机),我认为这就是增加整体延迟的原因。

我关注了 Google 的 Developer Android Guide on Cameras ,我的大部分代码都来自那里提供的示例。

我也在使用 ActionBarSherlock。

我目前有三个类(class):

相机管理器

public class CameraManager {

private static Camera CAMERA;

/** Check if this device has a camera */
public static boolean doesDeviceHaveCamera(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
return true;
}
else {
return false;
}
}

public static Camera getCameraInstance(){
CAMERA = null;
try {
CAMERA = Camera.open();
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return CAMERA;
}
}

相机预览

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private static final String TAG = "CameraPreview";
private SurfaceHolder surfaceHolder;
private Camera camera;

public CameraPreview(Context context, Camera camera) {
super(context);
this.camera = camera;
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // deprecated setting, but required on Android versions prior to 3.0
}

public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
}
catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.

if (surfaceHolder.getSurface() == null){
return;
}
try {
camera.stopPreview();
}
catch (Exception e){
// ignore: tried to stop a non-existent preview
}

// set preview size and make any resize, rotate or
// reformatting changes here
camera.setDisplayOrientation(90);
// start preview with new settings
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}
catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}

相机 Activity

public class CameraActivity extends SherlockActivity {

private static final String TAG = "CameraActivity";
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private Camera camera;
private CameraPreview preview;
private PictureCallback picture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions: media file is null.");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}
catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
}
catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.camera_view);

/**Get a camera instance**/
if(CameraManager.doesDeviceHaveCamera(this)){
camera = CameraManager.getCameraInstance();
}
else{
return;
}

/**Create a SurfaceView preview (holds the camera feed) and set it to the corresponding XML layout**/
preview = new CameraPreview(this, camera);
FrameLayout previewContainer = (FrameLayout) findViewById(R.id.camera_preview);
previewContainer.addView(preview);

/**Set up a capture button, which takes the picture**/
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
camera.takePicture(null, null, picture);
}
});
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyDirectory");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyDirectory", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
}
else {
return null;
}
return mediaFile;
}
}

我正在通过我的主 Activity 屏幕上的按钮调用 CameraActivity

最佳答案

The problem comes up when I take a picture and onPictureTaken() is called: it either doesn't work, or takes an absurdly long time for it to save a photo

这是一个大文件,您正在主应用程序线程上写入。

essentially, I just wanna access that raw byte array, without having to save it

那就不要保存了。 小心使用静态数据成员将字节数组放在主要 Activity 可以使用的地方(并且,当你完成它时,null 静态引用,所以内存可以被垃圾回收)。

关于android - OnPictureTaken() 计时/保存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21385084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com