大家好,我是通过这种方式使用 Intent 打开相机
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, ACTIVITY_CAMERA);
Run Code Online (Sandbox Code Playgroud)
很好,给了我完美的结果,但问题是这也会将图像保存到 SD 卡中,如何防止这种情况停止保存图像并将该数据用于onActivityResult()方法中
我想强制默认的 android 相机仅在横向模式下拍照。我试过以下:
Intent cameraIntent=new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "Pic.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
cameraIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
startActivity(cameraIntent);
Run Code Online (Sandbox Code Playgroud)
但这仅适用于 2.1,不适用于以后。我只想在横向模式下保存照片图像。我不想使用任何图像处理,例如矩阵或自定义相机。
注意:我从方向固定为“纵向”的活动中调用此意图
我的第一个问题!我有一段代码的问题,使用额外的输出选项启动摄像头意图,然后在活动结果尝试调整该图像的大小.发生的事情是在回调的resize函数中抛出一个nullpointer异常.
原始的大图片仍保存在文件系统中,因为我可以从文件系统访问它.
最初的相机jpeg尺寸为2560x1920,使用的手机是google nexus.
我不明白为什么调整大小不起作用,任何人都有任何见解?
这是一些代码:
takePicture函数也创建了虚拟文件:
public boolean takePicture( ) {
Log.e(TAG, "takePicture interface function");
String FileUri = Environment.getExternalStorageDirectory() + "/samples/";
File file = new File(FileUri,"picture"+ pictureNumber +".jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
Log.e(TAG, "intent started");
return true;
}
Run Code Online (Sandbox Code Playgroud)
on活动结果回调函数:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle the camera request returns and handle back button in camera. …Run Code Online (Sandbox Code Playgroud) 我已经几天寻找这个问题的解决方案,我不知道该怎么办:(
我有一个使用本机相机拍摄照片的应用程序,但是问题是,有时候,只有在某些设备中,当我拍摄照片并保存并返回后,我的应用程序才会重新启动。
它的webview大部分逻辑是在服务器端进行的,但是使用javascript接口来处理诸如相机之类的本机功能。
很难捕捉到它是因为它仅发生在某些设备中,即使它们是同一型号,我也拥有两个星系王牌,一个几乎总是发生而在其他情况下却很少(这些不是我测试过的唯一设备) )
我认为这与相机的方向或类似的东西有关。因为当我在“保存/丢弃”屏幕中旋转设备(拍照后)时,这种情况似乎更经常发生
这是我的一些代码:
表现:
<activity
android:name="XXX"
android:configChanges="orientation|keyboardHidden|screenSize"
android:excludeFromRecents="false"
android:label="@string/XXX"
android:launchMode="singleTask"
android:taskAffinity=""
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
camera.class
public void showNativeCamera(){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
UUID uniqueKey = UUID.randomUUID();
mPictureName = "img_" + uniqueKey.toString() + ".jpg";
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
Log.d("amanda", "file uri: " + fileUri);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // …Run Code Online (Sandbox Code Playgroud) 我有下一个问题:
当我尝试启动相机时,我可以拍摄照片,甚至将其保存在sdcard上,但是当我准备在设备上显示该照片的路径时,会出现错误。
我的全局变量为2(我使用了1,但最好使用2来确保这是一个奇怪的错误):
private File photofile;
private Uri mMakePhotoUri;
Run Code Online (Sandbox Code Playgroud)
这是我的入门相机功能:
@SuppressLint("SimpleDateFormat")
public void farefoto(int num){
// For naming the picture
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String n = sdf.format(new Date());
String fotoname = "Immagine-"+ n +".jpg";
//Going through files and folders
File photostorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File photostorage2 = new File(photostorage, "Immagini");
System.out.println(photostorage+"\n"+photostorage2);
photostorage2.mkdirs();
// My file (global)
photofile = new File(photostorage2, fotoname);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //intent to start camera
// My URI (global)
mMakePhotoUri = Uri.fromFile(photofile);
new Bundle(); …Run Code Online (Sandbox Code Playgroud) variables android global nullpointerexception android-camera-intent
我正在尝试使用以下方法识别图像的方向:
ExifInterface exif = new ExifInterface(_path);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Run Code Online (Sandbox Code Playgroud)
我不确定它是否工作正常。原因如下:我在我的桌子上以“纵向”方向拍摄一张纸的图像。我的电话在与桌子平行的平面上。但是,手机会保存图像,逆时针旋转 90 度。
我不知道为什么会这样,但无论如何,我希望我的exifOrientation为 6,以便我可以使用它来旋转我的图像:
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
...
...
}
Run Code Online (Sandbox Code Playgroud)
但是,我的exifOrientation是1并且图像没有正确旋转。
我看过: 为什么使用相机意图捕获的图像会在 Android 上的某些设备上旋转?
甚至尝试过:
private int getImageOrientation(){
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageColumns, null, null, imageOrderBy);
if(cursor.moveToFirst()){
int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
cursor.close();
return orientation;
} else {
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我已经浏览了几个文档和堆栈,但是我不太确定如何实现这个......
帮助或示例代码真的可以帮助我了解更多。
这是运行相机的代码集,它运行得非常好,我的下一个问题是,如何让它自动保存到手机图库中?
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tvCameraTiles = (TextView) findViewById(R.id.tvCameraTiles);
tvCameraTiles.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
//Get Camera Image File Data
// Bundle extras = data.getExtras();
// Bitmap imageBitmap = (Bitmap) …Run Code Online (Sandbox Code Playgroud) 最近优步发布了人脸识别功能,我正在尝试创建一个类似的相机预览,它看起来像这样:
我需要创建一个透明的圆圈来将用户的脸聚焦到相机上。我尝试从其他一些帖子中创建它,并尝试研究类似 Zxing 条码扫描器中的用例,但我仍然无法达到预期的结果
camera android face-recognition android-layout android-camera-intent
我不敢相信我已经为这个简单的问题苦苦挣扎了几个小时,但我无法让它正常运行。原则上我想要这个功能:
用户单击一个按钮。的ACTION_IMAGE_CAPTURE意图解雇,默认的相机应用打开。用户拍摄照片并返回到我的应用程序。我的应用程序显示缩略图。单击缩略图打开图像查看器。
其实很简单。有了这个指南,它似乎是一个复制和粘贴的问题:https : //developer.android.com/training/camera/photobasics.html。但魔鬼在细节中。
目前,它确实有效,但性能很糟糕,因为我RecyclerView即时创建了缩略图(在我的实际应用程序中,我为 a 中的每个条目显示了多个缩略图)。我希望在搜索数十个 Stack Overlow 问题时我忽略了一个简单的方法。
理想情况下,我想激发我的ACTION_IMAGE_CAPTURE意图,让默认的相机应用程序生成缩略图,将缩略图和图像存储在系统的缩略图和图像内容提供程序中,最后将相应的 content:// URI 取回onActivityResult(). 那就太好了……
现在,这是我面临的问题:
getData()结果意图时收到的 URIonActivityResult()始终null是我是否MediaStore.EXTRA_OUTPUT与调用意图一起提供值MediaStore.EXTRA_OUTPUT额外内容时,我以位图的形式而不是 content:// URI 的形式获取缩略图。因此,如果我不想在下次开始活动时生成缩略图,我必须自己保留缩略图。所以,我想我希望两个 content:// URI 不是它的工作原理。但在我继续自己生成(最终复制)缩略图并将它们持久化并缓存在我的应用程序中之前,我想问:
有什么我不知道的简单方法吗?是否可以同时检索图像和缩略图,还是必须自己生成缩略图?
注意:我希望通过让 Android 将我的应用程序拍摄的照片添加到媒体数据库中,将其提供给图库。我无法想象访问该媒体数据库(如画廊)的每个应用程序都会创建它自己的一组缩略图,对吗?
提前致谢!