gpt4 book ai didi

java - Camera.takePicture(null, null, mPicture) 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:00 26 4
gpt4 key购买 nike

我关注https://developer.android.com/guide/topics/media/camera.html激活 android 4 的自定义相机,但我的拍摄功能完全不起作用。下面是我的代码:

cameraf.java:

public class cameraf extends AppCompatActivity{
private Preview mPreview;
private Camera mCamera;
private static final String TAG = "Myact";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cameraf);
mCamera = getCameraInstance();
mPreview = new Preview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
Log.v(TAG, "will now take picture");
mCamera.takePicture(null, null, mPicture);
return true;
}
});
preview.addView(mPreview);
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.v(TAG, "Getting output media file");
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.v(TAG, "Error creating output file");
return;}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.v(TAG, e.getMessage());
} catch (IOException e) {
Log.v(TAG, e.getMessage());}
}
};
public static final int MEDIA_TYPE_IMAGE = 1;
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;}
return mediaFile;
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
}
return c; // returns null if camera is unavailable
}
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public Preview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
Camera.Parameters params = mCamera.getParameters();
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
} 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();
parameters.setPreviewSize(w, h);
mCamera.startPreview();
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}

我已经在 list 中添加了权限,即 uses-permission android:name="android.permission.CAMERA",uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

但是从上面的代码来看,它可以让我打开相机,但是当我点击屏幕时,它会出错并且无法存储图像。那么有人可以分享我的想法吗?

错误: enter image description here

最佳答案

您看到的失败可能是因为您没有设置图片大小而发生的。有些设备需要这个。

请注意,您必须选择支持的尺寸之一,并确保图片尺寸和预览尺寸同步(即具有相同的宽高比)。

您无法设置从表面 View 尺寸派生的预览尺寸……实际上,您不这样做:您的电话

parameters.setPreviewSize(w, h);

后面没有

mCamera.setParameters(parameters);

关于java - Camera.takePicture(null, null, mPicture) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49218220/

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