【问题标题】:How to take a picture without an Intent and without any view window in Android如何在没有 Intent 且没有任何视图窗口的情况下在 Android 中拍照
【发布时间】:2014-09-27 07:37:01
【问题描述】:

大家好,我想弄清楚如何通过按下按钮来拍照,但没有显示任何预览。这个想法是,我想要拍摄并保存一张照片,但在之前或之后没有照片的视觉预览。到目前为止,我能够毫无问题地获取用于拍照并将它们保存到磁盘的代码,但如果没有表面视图或预览,我似乎无法做到这一点。

这是我的一些代码:

主要活动:

public class MainActivity extends Activity implements OnClickListener {

        private Camera cameraObject;
        private ShowCamera showCamera;
        private Button NOPE;

        //Check if camera is avail:
        public static Camera isCameraAvailiable(){
            Camera object = null;
            try {
              object = Camera.open();
                L.m("Camera Open");
            } catch (Exception e) {
              L.m(e.toString());
            } return object; 
        }

       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          //Opens up the camera
          cameraObject = isCameraAvailiable();

          //Sets the resolution for the camera (Excluded from code here)
          setCameraResolution();

          //Button for taking photos
          NOPE = (Button) findViewById(R.id.button_capture);
          NOPE.setOnClickListener(this);

          //THIS SECTION OF CODE HERE I can't get it to work without it as this creates a view/ preview for the camera
          showCamera = new ShowCamera(this, cameraObject);
          FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
          preview.addView(showCamera);
       }

        public void snapIt(View view){
           cameraObject.takePicture(null, null, new PhotoHandler(getApplicationContext()));
       }

        public void onClick(View view) {
            switch (view.getId()){
            case R.id.button_capture:
                    snapIt(view);
            }
        }   
    }

照片处理类:

public class PhotoHandler implements PictureCallback {

    private final Context context;

    public PhotoHandler(Context context) {
        this.context = context;
    }

    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFileDir = getDir();
        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
            //I removed unnecessary code here, but this is where I write to disk, which works fine.
        }
    }

我遇到的问题是,除非我有关于 preview.addView(showCamera); 的代码,否则我实际上无法通过相机拍照。 ShowCamera 类只是添加了一个表面视图,以便在拍摄时查看图片:

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback

有人有什么想法吗?这个可以吗?

有人在这里问了一个非常相似的问题:Taking picture without SurfaceView or without Photo Intent Activity,但没有任何成功。我想我和他们走的是相似的道路。

【问题讨论】:

    标签: android image android-intent camera


    【解决方案1】:

    我有同样的问题,我通过在 SurficeView 上放置新视图来解决它,因此看不到 surficeView。我花了很多时间寻找其他解决方案,但没有成功。

    【讨论】:

    • 这是有道理的,我最终做了同样的事情并将它压缩到 1px x 1px,这有点低效,因为它仍然使用 cpu,但它完成了工作。感谢您的提示!
    猜你喜欢
    • 2012-12-20
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多