没有预览/ UI的Android 4.4相机拍摄

时间:2014-06-09 06:30:24

标签: android user-interface camera preview

我试图在Nexus 5(Android 4.4.3,股票)上触发照片捕捉,而不会触发任何UI或预览。我已经在这个网站和其他网站上看到了几种方法,但它们似乎是特定于设备或与最新版本的Android不兼容。我对Android开发还很陌生,所以我可能会遗漏一些基本的东西,我真的很感激提供的任何帮助。

这是我的代码:

package com.example.omnisense;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.os.Environment;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Sensor_Camera {
    private Camera mCamera;
    private Context context;
    private SurfaceView sv;
    private Parameters parameters;

    private int width;
    private int height;
    private Size size;

    public Sensor_Camera(Context c_){
        context = c_;
        sv = new SurfaceView(context);
        if (Camera.getNumberOfCameras() >= 2) {
            mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT);
        } else {
            mCamera = Camera.open();
        }       


    }

    public void takePicture() {
        try {
            mCamera.setPreviewDisplay(sv.getHolder());
            parameters = mCamera.getParameters();
            size = parameters.getPictureSize();
            width = size.width;
            height = size.height;
            // Default picture format is NV21
            mCamera.setParameters(parameters);

            mCamera.startPreview();

            mCamera.takePicture(null,  null, mCall);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Camera.PictureCallback mCall = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(final byte[] data, Camera camera) {
                // Do Stuff
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
        }
    };      

}

以下是我发现引用的问题/答案/博文:

Invisible SurfaceView for Camera Preview

http://cell0907.blogspot.com/2014/01/android-camera-capture-without.html

Android: Take Photo Without User Interface

这是相关的logcat输出:

06-09 01:25:22.831: D/Main(20056): onClick
06-09 01:25:22.831: D/Main(20056): testButton Click
06-09 01:25:23.111: D/Camera(20056): app passed NULL surface
06-09 01:25:23.121: D/AndroidRuntime(20056): Shutting down VM
06-09 01:25:23.121: W/dalvikvm(20056): threadid=1: thread exiting with uncaught exception (group=0x41d2bba8)
06-09 01:25:23.121: E/AndroidRuntime(20056): FATAL EXCEPTION: main
06-09 01:25:23.121: E/AndroidRuntime(20056): Process: com.example.omnisense, PID: 20056
06-09 01:25:23.121: E/AndroidRuntime(20056): java.lang.RuntimeException: takePicture failed
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.hardware.Camera.native_takePicture(Native Method)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.hardware.Camera.takePicture(Camera.java:1244)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.hardware.Camera.takePicture(Camera.java:1189)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at com.example.omnisense.Sensor_Camera.takePicture(Sensor_Camera.java:60)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at com.example.omnisense.MainActivity.testButtonPress(MainActivity.java:172)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at com.example.omnisense.MainActivity$2.onClick(MainActivity.java:235)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.view.View.performClick(View.java:4438)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.view.View$PerformClick.run(View.java:18422)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.os.Handler.handleCallback(Handler.java:733)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.os.Looper.loop(Looper.java:136)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at android.app.ActivityThread.main(ActivityThread.java:5017)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at java.lang.reflect.Method.invokeNative(Native Method)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at java.lang.reflect.Method.invoke(Method.java:515)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
06-09 01:25:23.121: E/AndroidRuntime(20056):    at dalvik.system.NativeStart.main(Native Method)

在我的主要活动中,此类与以下代码一起使用:

Sensor_Camera sense_camera = new Sensor_Camera(this);
sense_camera.takePicture();

我也为相机添加了uses-permission和uses-feature。我不确定问题是什么,虽然我认为它与使用null SurfaceView有关。有没有解决方法,或者在4.4中是不可能的?

如果我还有其他任何关于我的代码的帖子,请告诉我!提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

查看the documentation,您正在使用postview回调,他们会注意到

  

在缩放的,完全处理的后期审核时发生postview回调   图像可用(注意:并非所有硬件都支持此功能)。

听起来你的设备可能不支持,所以你可能需要使用jpeg回调(第4个可选参数)。