android - 无需用户交互即可自动拍照

标签 android camera android-camera-intent

我使用此代码从相机捕获图像。

package android.takeowneship;


import java.io.File;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ImageView;
import android.widget.Toast;


public class camera extends Activity{


    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

    Uri imageUri;
    private ImageView imageView;

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

          //define the file-name to save photo taken by Camera activity
            String fileName = "new-photo-name.jpg";
            //create parameters for Intent with filename
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
            //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
            imageUri = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            //create new Intent
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

        }

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {

         if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
             if (resultCode == RESULT_OK) {
                 //use imageUri here to access the image
                 Toast.makeText(this, "picture has been taken"+ imageUri, Toast.LENGTH_SHORT).show();


             } else if (resultCode == RESULT_CANCELED) {
                 Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
             } else {
                 Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
             }
         }

         }

     public static File convertImageUriToFile (Uri imageUri, Activity activity)  {

         Cursor cursor = null;
         try {
             String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
             cursor = activity.managedQuery( imageUri,
                     proj, // Which columns to return
                     null,       // WHERE clause; which rows to return (all rows)
                     null,       // WHERE clause selection arguments (none)
                     null); // Order-by clause (ascending by name)
             int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//           int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
             if (cursor.moveToFirst()) {
//               String orientation =  cursor.getString(orientation_ColumnIndex);
                 return new File(cursor.getString(file_ColumnIndex));
             }
             return null;
         } finally {
             if (cursor != null) {
                 cursor.close();
             }
         }
         }

但在此代码中,它会打开相机,用户必须单击按钮才能拍照。 我想要的是自动拍摄照片(无需预览)并将其保存在存储卡中。

最佳答案

Android 不允许您在不显示预览窗口的情况下拍照。所以你必须让表面 View 非常小。喜欢1*1 像素并将其放在任意控件的一角

或者显示一个虚拟表面 View 来执行此操作。

SurfaceView view = new SurfaceView(this);
c.setPreviewDisplay(view.getHolder());
c.startPreview();
c.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback);

检查(删除损坏的链接)thisthis出。

关于android - 无需用户交互即可自动拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9752730/

相关文章:

android - 有没有一种有效的方法来进行 "continuous"自动对焦?

android - 在前置摄像头上拉伸(stretch)相机预览

android - 在fragment中调用Camera Service,如何处理回调?

android - 加载 native C 库时出错,用于 Android 中的 PDF 查看

android - 按下返回键后Android Activity的生命周期

c# - 用于照片捕获设备的视频刷的旋转

Android - 撤销权限 android.permission.CAMERA

android - 从项目中删除 Cordova 插件

android - npx react-native run-android 不起作用 - "java.io.IOException: The filename, directory name, or volume label syntax is incorrect"

android - 如何允许从图库和相机将多个图像上传到 webview android