我正在尝试使用Camera2创建一个手电筒.我收到了错误.当我试图打开或关闭手电筒时,我收到错误消息:
android.harDWare.camera2.CameraAccessExeption: The camera device has
encountered a serIoUs error.
主要活动
package com.nettport.lommelykt;import androID.Manifest;import androID.content.pm.PackageManager;import androID.support.v4.app.ActivityCompat;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import androID.Widget.Togglebutton;import androID.content.Context;import androID.graphics.SurfaceTexture;import androID.harDWare.camera2.CameraAccessException;import androID.harDWare.camera2.CameraCaptureSession;import androID.harDWare.camera2.Cameracharacteristics;import androID.harDWare.camera2.CameraDevice;import androID.harDWare.camera2.CameraManager;import androID.harDWare.camera2.CameraMetadata;import androID.harDWare.camera2.CaptureRequest;import androID.util.Size;import androID.vIEw.Surface;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { CameraManager cameraManager; Cameracharacteristics cameracharacteristics; CameraDevice mCameraDevice; CameraCaptureSession mSession; CaptureRequest.Builder mBuilder; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // Initialize camera initCamera(); // Click button on or off Togglebutton onOffbutton = (Togglebutton) findVIEwByID(R.ID.togglebutton); onOffbutton.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // your click actions go here if (((Togglebutton) v).isChecked()) { changeTextAndIcon(1); turnOnFlashlight(); } else { changeTextAndIcon(0); turnOffFlashlight(); } } }); } /*- Change text and icon ------------------------------------- */ // modus = 1 -> turn on // modus = 0 -> turn off voID changeTextAndIcon(int modus) { // Change text TextVIEw statusText = (TextVIEw) findVIEwByID(R.ID.textVIEw); if (modus == 1) { statusText.setText("Lommelykten er på"); // Flash is on } else { statusText.setText("Lommelykten er av"); // Flash is off } // Change icon to off ImageVIEw flashlightIcon = (ImageVIEw) findVIEwByID(R.ID.imageVIEw); if (modus == 1) { flashlightIcon.setimageResource(R.drawable.flashlight_on); } else { flashlightIcon.setimageResource(R.drawable.flashlight_off); } } private voID initCamera() { cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { String[] ID = cameraManager.getCameraIDList(); if (ID != null && ID.length > 0) { cameracharacteristics = cameraManager.getCameracharacteristics(ID[0]); boolean isFlash = cameracharacteristics.get(Cameracharacteristics.FLASH_INFO_AVAILABLE); if (isFlash) { 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; } cameraManager.openCamera(ID[0], new MyCameraDeviceStateCallback(), null); Toast.makeText(this, "Camera initialized", Toast.LENGTH_LONG).show(); } } } catch (CameraAccessException e) { e.printstacktrace(); Toast.makeText(this, "Camera initialization error:\n" + e.toString(), Toast.LENGTH_LONG).show(); } } class MyCameraDeviceStateCallback extends CameraDevice.StateCallback { @OverrIDe public voID onopened(CameraDevice camera) { mCameraDevice = camera; // get builder try { mBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); List<Surface> List = new ArrayList<Surface>(); SurfaceTexture mSurfaceTexture = new SurfaceTexture(1); Size size = getSmallestSize(mCameraDevice.getID()); mSurfaceTexture.setDefaultBufferSize(size.getWIDth(), size.getHeight()); Surface mSurface = new Surface(mSurfaceTexture); List.add(mSurface); mBuilder.addTarget(mSurface); camera.createCaptureSession(List, new MyCameraCaptureSessionStateCallback(), null); } catch (CameraAccessException e) { e.printstacktrace(); } } @OverrIDe public voID ondisconnected(CameraDevice camera) { } @OverrIDe public voID one rror(CameraDevice camera, int error) { } } private Size getSmallestSize(String cameraID) throws CameraAccessException { Size[] outputSizes = cameraManager.getCameracharacteristics(cameraID).get(Cameracharacteristics.SCALER_STREAM_CONfigURATION_MAP).getoutputSizes(SurfaceTexture.class); if (outputSizes == null || outputSizes.length == 0) { throw new IllegalStateException("Camera " + cameraID + "doesn't support any outputSize."); } Size chosen = outputSizes[0]; for (Size s : outputSizes) { if (chosen.getWIDth() >= s.getWIDth() && chosen.getHeight() >= s.getHeight()) { chosen = s; } } return chosen; } class MyCameraCaptureSessionStateCallback extends CameraCaptureSession.StateCallback { @OverrIDe public voID onConfigured(CameraCaptureSession session) { mSession = session; try { mSession.setRepeatingRequest(mBuilder.build(), null, null); } catch (CameraAccessException e) { e.printstacktrace(); } } @OverrIDe public voID onConfigureFailed(CameraCaptureSession session) { } } public voID turnOnFlashlight() { try { mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH); mSession.setRepeatingRequest(mBuilder.build(), null, null); Toast.makeText(this, "Flash activated", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printstacktrace(); Toast.makeText(this, "Flash Failed:\n" + e.toString(), Toast.LENGTH_LONG).show(); } } public voID turnOffFlashlight() { try { mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF); mSession.setRepeatingRequest(mBuilder.build(), null, null); Toast.makeText(this, "Flash Disabled", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printstacktrace(); Toast.makeText(this, "Flash Failed:\n" + e.toString(), Toast.LENGTH_LONG).show(); } } private voID close() { Toast.makeText(this, "Camera closed", Toast.LENGTH_LONG).show(); if (mCameraDevice == null || mSession == null) { return; } mSession.close(); mCameraDevice.close(); mCameraDevice = null; mSession = null; }}
AndroIDManifest
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.nettport.lommelykt"> <application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:supportsRtl="true" androID:theme="@style/Apptheme" > <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <!-- Allows access to the flashlight --> <uses-permission androID:name="androID.permission.CAMERA" /> <uses-permission androID:name="androID.permission.FLASHliGHT" /> <uses-feature androID:name="androID.harDWare.camera" /> <uses-feature androID:name="androID.harDWare.camera.autofocus" /> <uses-feature androID:name="androID.harDWare.camera.flash" /></manifest>
解决方法:
你没有坚持你的SurfaceTexture;从它创建的Surface大致等于弱指针,并且不会使SurfaceTexture不被垃圾收集.
查看在MainActivity中存储SurfaceTexture是否可以解决您的问题.
如果没有,您将需要提供崩溃的logcat(不仅仅是应用程序日志记录,整个系统),以更详细地查看相机服务正在抱怨的内容.
以上是内存溢出为你收集整理的尝试使用Android Camera2创建手电筒全部内容,希望文章能够帮你解决尝试使用Android Camera2创建手电筒所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)