我最近开发了一个特殊的相机预览,因为我需要取消镜像默认预览。代码如下:
public class CameraApi extends AppCompatActivity {
private static final String TAG = "CameraAPI";
protected TextureView textureView;
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
private String cameraId;
protected CameraDevice cameraDevice;
protected CameraCaptureSession cameraCaptureSessions;
protected CaptureRequest captureRequest;
protected CaptureRequest.Builder captureRequestBuilder;
protected Size imageDimension;
private ImageReader imageReader;
private File file;
private static final int REQUEST_CAMERA_PERMISSION = 200;
private boolean mFlashSupported;
private Handler mBackgroundHandler;
private HandlerThread mBackgroundThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_full_screen);
textureView = (TextureView) findViewById(R.id.texture_revcam);
assert textureView != null;
textureView.setSurfaceTextureListener(textureListener);
}
protected TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
//create a matrix to invert the x-plane, i.e undo the mirror
Matrix matrix = new Matrix();
matrix.setScale(-1, 1);
//move it back to in view otherwise it'll be off to the left.
matrix.postTranslate(width, 0);
textureView.setTransform(matrix);
//open your camera here
openCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Transform you image captured size according to the surface width and height
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
//This is called when the camera is open
Log.e(TAG, "onOpened");
cameraDevice = camera;
createCameraPreview();
}
@Override
public void onDisconnected(CameraDevice camera) {
cameraDevice.close();
}
@Override
public void onError(CameraDevice camera, int error) {
cameraDevice.close();
cameraDevice = null;
}
};
final CameraCaptureSession.CaptureCallback captureCallbackListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Toast.makeText(CameraApi.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
createCameraPreview();
}
};
protected void startBackgroundThread() {
mBackgroundThread = new HandlerThread("Camera Background");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
protected void stopBackgroundThread() {
mBackgroundThread.quitSafely();
try {
mBackgroundThread.join();
mBackgroundThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void createCameraPreview() {
try {
SurfaceTexture texture = textureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
Surface surface = new Surface(texture);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback(){
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
//The camera is already closed
if (null == cameraDevice) {
return;
}
// When the session is ready, we start displaying the preview.
cameraCaptureSessions = cameraCaptureSession;
updatePreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
Toast.makeText(CameraApi.this, "Configuration change", Toast.LENGTH_SHORT).show();
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void openCamera() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
Log.e(TAG, "is camera open");
try {
cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
// Add permission for camera and let user grant the permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(CameraApi.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
return;
}
manager.openCamera(cameraId, stateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
Log.e(TAG, "openCamera X");
}
protected void updatePreview() {
if(null == cameraDevice) {
Log.e(TAG, "updatePreview error, return");
}
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
try {
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void closeCamera() {
if (null != cameraDevice) {
cameraDevice.close();
cameraDevice = null;
}
if (null != imageReader) {
imageReader.close();
imageReader = null;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// close the app
Toast.makeText(CameraApi.this, "Sorry!!!, you can't use this app without granting permission", Toast.LENGTH_LONG).show();
finish();
}
}
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume");
startBackgroundThread();
if (textureView.isAvailable()) {
openCamera();
} else {
textureView.setSurfaceTextureListener(textureListener);
}
}
@Override
protected void onPause() {
Log.e(TAG, "onPause");
//closeCamera();
stopBackgroundThread();
super.onPause();
}
public void hideSystemUI() {
//for new api versions.
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(8);
}
public void checkCameraPermissions() {
final boolean cameraGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
final boolean audioGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
final boolean audioNeeded = true;
String[] perms = null;
if (cameraGranted) {
if (audioNeeded && !audioGranted) {
perms = new String[]{Manifest.permission.RECORD_AUDIO};
}
} else {
if (audioNeeded && !audioGranted) {
perms = new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};
} else {
perms = new String[]{Manifest.permission.CAMERA};
}
}
if (perms != null) {
ActivityCompat.requestPermissions(this, perms, 69);
}
}
}
我只想在点击按钮时切换到全屏,但它会崩溃应用程序。我尝试过这样的事情:
cpHover.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder loadingFullscreen = new Builder(IdleModeFragment.this.getContext());
loadingFullscreen.setMessage(getString(R.string.load_fullscreen));
AlertDialog dialog = loadingFullscreen.show();
Intent intent = new Intent(getContext(),CameraApi.class);
startActivity(intent);
}
});
我不知道是否有另一种方式,原则上我只想创建一个新的相机实例(我想),然后切换到另一个选项卡(意思是另一个窗口)有预览的容器。
提前致谢!
编辑:日志
03-09 11:41:46.936 8432-8432/com.microlay.nanodlp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.microlay.nanodlp, PID: 8432
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.microlay.nanodlp/com.microlay.nanodlp.utils.CameraApi}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30)
at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:146)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:932)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047)
at android.support.v4.app.Fragment.startActivity(Fragment.java:940)
at android.support.v4.app.Fragment.startActivity(Fragment.java:929)
at com.microlay.nanodlp.fragments.IdleModeFragment$2.onClick(IdleModeFragment.java:168)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
编辑2:黑屏会抛出此错误
03-09 13:10:52.838 28985-28985/com.microlay.nanodlp E/CameraAPI: onResume
03-09 13:10:52.852 28985-28985/com.microlay.nanodlp W/TextureView: A TextureView or a subclass can only be used with hardware acceleration enabled.
03-09 13:10:53.269 28985-28985/com.microlay.nanodlp E/WindowManager: android.view.WindowLeaked: Activity com.microlay.nanodlp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{24cf85c6 V.E..... R.....I. 0,0-520,77} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at android.app.AlertDialog$Builder.show(AlertDialog.java:993)
at com.microlay.nanodlp.fragments.IdleModeFragment$2.onClick(IdleModeFragment.java:166)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
答案 0 :(得分:0)
如错误日志中所述,您尚未在清单中声明自己的活动。 在manifest.xml中添加此行
<activity android:name="utils.CameraApi" />
答案 1 :(得分:0)
正如我在评论中所解释的那样,您不应该显示对话框,然后启动另一个活动。这就是WindowLeaked
消息的全部内容。为了避免这种情况,您应该关闭任何打开的对话框 - 或者根本不打开这个对话框:
AlertDialog.Builder loadingFullscreen = new Builder(IdleModeFragment.this.getContext());
loadingFullscreen.setMessage(getString(R.string.load_fullscreen));
AlertDialog dialog = loadingFullscreen.show();
如果你真的想要显示一个对话框,然后启动另一个Activity,你可以这样做:
Intent intent = new Intent(getContext(), CameraApi.class);
new AlertDialog.Builder(this)
.setMessage(getString(R.string.load_fullscreen))
.setPositiveButton("OK", (dialog, which) -> startActivity(intent);)
.setNegativeButton(null, null).create().show();
一旦用户解除对话框,其他活动就会启动。因此,没有窗户会被泄露。