我試圖觸發Nexus 5(Android 4.4.3,股票)上的圖片捕獲,而不觸發任何UI或預覽。我已經在這個網站和其他網站上看到了幾種方法,但它們似乎是設備特定的,或者與最新版本的Android不兼容。我對Android開發相當陌生,所以我可能會錯過一些相當基本的東西,我非常感謝所提供的幫助。Android 4.4沒有預覽/ UI的攝像頭捕獲
這裏是我的代碼:
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
一個d這裏的相關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();
我已經添加了使用許可權和使用特徵的相機好。我不確定問題是什麼,雖然我認爲它與使用空的SurfaceView有關。是否有任何解決方法,或者在4.4中是不可能的?
如果還有其他事情我應該發佈我的代碼,請讓我知道!預先感謝任何和所有幫助。
謝謝你的建議。我會試試這個。 –