android 本地窗口,SurfaceView - > Surface沒有有效的本地窗口| Android Camera API | hardware.camera2...
我已經通過兩種方式處理了這個問題,一個是在XML中創建SurfaceView,然後將它的值賦給我的私有SurfaceView或使用公共構造函數。使用該SurfaceView,我可以使用getHolder()獲取SurfaceHolder,然後使用getSurface()獲取Surface。在我發佈的代碼中,我只是用公共構造函數創建它。這裏是我的代碼,其快速的類實現我昨天與其他項目嘗一嘗後嘗試。檢查
我已經通過兩種方式處理了這個問題,一個是在XML中創建SurfaceView,然後將它的值賦給我的私有SurfaceView或使用公共構造函數。
使用該SurfaceView,我可以使用getHolder()獲取SurfaceHolder,然後使用getSurface()獲取Surface。
在我發佈的代碼中,我只是用公共構造函數創建它。
這裏是我的代碼,其快速的類實現我昨天與其他項目嘗一嘗後嘗試。檢查MainActivity的onCreate()和onOpened(),使用SurfaceView。
package com.example.universitywork.canyouwork;
/* IMPORTS */
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
// CREAMOS CLASES
private CameraManager mManager;
private CameraDevice cDevice;
private CaptureRequest cRequest;
private Handler handle = new Handler();
private SurfaceView surface;
private SurfaceHolder holder;
private Surface under;
private final CameraDevice.StateCallback mCallbackDevice = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
cDevice = camera;
try {
camera.createCaptureSession(Arrays.asList(under), mCallbackSession, handle);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(CameraDevice camera) {
System.out.println("ONDC");
}
@Override
public void onError(CameraDevice camera, int error) {
System.out.println("ONerror");
}
};
private final CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {
};
private final CameraCaptureSession.StateCallback mCallbackSession = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
System.out.println("LLEGO AQUI");
cRequest = cDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW).build();
// session.capture(cRequest, mCaptureCallback, handle);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
System.out.println("ONCONFIGUREFAILED");
}
};
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surface= new SurfaceView(this);
holder = surface.getHolder();
holder.addCallback(this);
holder.setFixedSize(500,500);
under= holder.getSurface();
mManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String id = getId(mManager);
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mManager.openCamera(id, mCallbackDevice, handle);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
System.out.println("ONCHANGED");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("ONDESTROYED");
}
public String getId (CameraManager manager) {
try {
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
int orientation = characteristics.get(CameraCharacteristics.LENS_FACING);
if (orientation == CameraCharacteristics.LENS_FACING_FRONT) return cameraId;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
return "NULL";
}}
快進到錯誤:
E/Legacy-CameraDevice-JNI: getNativeWindow: Surface had no valid native window.
E/Legacy-CameraDevice-JNI: LegacyCameraDevice_nativeDetectSurfaceDimens: Could not retrieve native window from surface.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.universitywork.canyouwork, PID: 4885
java.lang.IllegalArgumentException: Surface was abandoned
at android.hardware.camera2.utils.SurfaceUtils.getSurfaceSize(SurfaceUtils.java:70)
at android.hardware.camera2.params.OutputConfiguration.(OutputConfiguration.java:97)
at android.hardware.camera2.params.OutputConfiguration.(OutputConfiguration.java:71)
at android.hardware.camera2.impl.CameraDeviceImpl.createCaptureSession(CameraDeviceImpl.java:474)
at com.example.universitywork.canyouwork.MainActivity$1.onOpened(MainActivity.java:40)
at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:134)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.hardware.camera2.legacy.LegacyExceptionUtils$BufferQueueAbandonedException
at android.hardware.camera2.legacy.LegacyExceptionUtils.throwOnError(LegacyExceptionUtils.java:64)
at android.hardware.camera2.legacy.LegacyCameraDevice.getSurfaceSize(LegacyCameraDevice.java:540)
at android.hardware.camera2.utils.SurfaceUtils.getSurfaceSize(SurfaceUtils.java:68)
at android.hardware.camera2.params.OutputConfiguration.(OutputConfiguration.java:97)
at android.hardware.camera2.params.OutputConfiguration.(OutputConfiguration.java:71)
at android.hardware.camera2.impl.CameraDeviceImpl.createCaptureSession(CameraDeviceImpl.java:474)
at com.example.universitywork.canyouwork.MainActivity$1.onOpened(MainActivity.java:40)
at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:134)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我認爲這與大小或SurfaceView的下墊面做。也許我沒有正確使用它,但Android文檔說它可以用SurfaceView製作。
2016-12-29
HeTheMan
更多推荐
所有评论(0)