gpt4 book ai didi

java - 如何强制用户使用前置摄像头?

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

我正在开发一个使用相机的程序。我正在使用媒体商店从 Android 相机获取原始素材。

我希望用户必须使用前置摄像头,或者至少默认打开前置摄像头。

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                      
cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

我已经尝试过这个,以及另一种调用方法:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                    
cameraIntent.putExtra("android.intent.extras.CAMERA_FACING",
android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
cameraIntent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

有什么想法可以解释为什么这不起作用吗?我在华硕平板电脑上使用它。

谢谢!

最佳答案

尝试这样:

int MEDIA_TYPE_IMAGE = 1;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri = Uri.fromFile(getOutputPhotoFile(MEDIA_TYPE_IMAGE));
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
startActivityForResult(intent, CAMERA_PHOTO_REQUEST_CODE);

在 AndroidManifest.xml 文件中添加这些权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>


private static File getOutputMediaFile(int type) {

// External sdcard location
File mediaStorageDir = new File(storage);

// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(storage, "Oops! Failed create "
+ storage + " directory");
return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).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;
}

关于java - 如何强制用户使用前置摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51242723/

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