相机错误2 android 6.0

时间:2016-06-15 12:04:51

标签: android camera android-camera android-6.0-marshmallow

请帮助理解含义:E / Camera:错误2。 我的相机代码不仅适用于Android 6.0,另一个适用。此代码用于扫描qr代码。在6.0预览中不包括但您可以包括闪光灯 运行时权限处于活动状态。 有时:W / System.err:java.lang.RuntimeException:onPreviewFrame中的getParameters失败(空参数)。

SimpleCameraView:

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.graphics.Point;
import android.hardware.Camera;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import java.util.List;

public class SimpleCameraView extends SurfaceView implements SurfaceHolder.Callback {
 private SurfaceHolder surfaceHolder;
 private Camera camera;
 private Camera.PreviewCallback previewCallback;
 private Display display;

 public SimpleCameraView(Context context, Camera.PreviewCallback previewCallback) {
 super(context);
 this.previewCallback = previewCallback;
 this.display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
 this.surfaceHolder = this.getHolder();
 this.surfaceHolder.addCallback(this);
 this.surfaceHolder.setType(3);
 this.setKeepScreenOn(true);
 this.configureCamera(this.getResources().getConfiguration());
 }

 public Camera getCamera() {
 try {
 this.camera = Camera.open();
 //this.camera.lock();
 Log.e("1111111","getCamera");
 } catch (Exception var2) {
 var2.printStackTrace();
 }

 return this.camera;
 }

 public void surfaceCreated(SurfaceHolder holder) {
 try {
 this.camera.setPreviewDisplay(holder);
 Log.e("1111111","surfaceCreated");
 } catch (Exception var3) {
 var3.printStackTrace();
 }

 }

 public void surfaceDestroyed(SurfaceHolder holder) {

 this.stopCamera();
 Log.e("1111111","surfaceDestroy");
 }

 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
 startCamera();
 Log.e("1111111","surfaceChanged");
 }

 public boolean configureCamera(Configuration configuration) {
 try {
 this.getCamera();
 if (this.camera != null) {
 int e = this.getScreenWidth();
 int height = this.getScreenHeight();
 int displayOrientationDegrees = this.getDisplayOrientationDegrees(this.display);
 this.camera.setDisplayOrientation(displayOrientationDegrees);
 Camera.Size previewSize = this.camera.getParameters().getPreviewSize();
 float aspect = (float) previewSize.width / (float) previewSize.height;
 ViewGroup.LayoutParams cameraHolderParams = new RelativeLayout.LayoutParams(480,640);
 if (configuration.orientation == 1) {
 cameraHolderParams.height = height;
 cameraHolderParams.width = (int) ((float) height / aspect);
 } else {
 cameraHolderParams.width = e;
 cameraHolderParams.height = (int) ((float) e / aspect);
 }

 this.setLayoutParams(cameraHolderParams);
 Log.e("1111111","configureCamera");
 return true;
 }
 } catch (Exception var8) {
 var8.printStackTrace();
 }

 return false;
 }

 private int getScreenWidth() {
 if (Build.VERSION.SDK_INT < 13) {
 return this.display.getWidth();
 } else {
 Point size = new Point();
 this.display.getSize(size);
 return size.x;
 }
 }

 private int getScreenHeight() {
 if (Build.VERSION.SDK_INT < 13) {
 return this.display.getHeight();
 } else {
 Point size = new Point();
 this.display.getSize(size);
 return size.y;
 }
 }

 private int getDisplayOrientationDegrees(Display display) {
 int orientation = this.getResources().getConfiguration().orientation;
 short displayOrientationDegrees;
 switch (display.getRotation()) {
 case 0:
 if (orientation == 1) {
 displayOrientationDegrees = 90;
 } else {
 displayOrientationDegrees = 0;
 }
 break;
 case 1:
 if (orientation == 2) {
 displayOrientationDegrees = 0;
 } else {
 displayOrientationDegrees = 270;
 }
 break;
 case 2:
 if (orientation == 1) {
 displayOrientationDegrees = 270;
 } else {
 displayOrientationDegrees = 180;
 }
 break;
 case 3:
 if (orientation == 2) {
 displayOrientationDegrees = 180;
 } else {
 displayOrientationDegrees = 90;
 }
 break;
 default:
 displayOrientationDegrees = 0;
 }

 return displayOrientationDegrees;
 }

 public void stopCamera() {
 try {
 this.camera.stopPreview();
 this.camera.setPreviewCallback((Camera.PreviewCallback) null);
 this.camera.release();
 this.camera = null;
 Log.e("1111111","stopCamera");
 } catch (Exception var2) {
 var2.printStackTrace();
 }

 }

 public void startCamera() {
 try {
 if (this.surfaceHolder.getSurface() == null) {
 Log.e("1111111","null surface");
 return;
 }
 this.camera.reconnect();
 this.camera.setPreviewDisplay(this.surfaceHolder);
 if (this.previewCallback != null) {
 this.camera.setPreviewCallback(this.previewCallback);
 }
 Camera.CameraInfo info = new Camera.CameraInfo();
 Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
 int rotate = (info.orientation + 360) % 360;
 Camera.Parameters params = camera.getParameters();
 params.setJpegQuality(50);
 params.setPictureFormat(ImageFormat.JPEG);
 List<Camera.Size> sizes = params.getSupportedPictureSizes();
 Camera.Size size = sizes.get(0);
 /*for (int i=0;i<sizes.size();i++)
 if (sizes.get(i).width > 1000 && sizes.get(i).width<1500)
 if (sizes.get(i).height < 2000 && sizes.get(i).height> 1500)
 size = sizes.get(i);*/
 if (size.width > 480) {
 for (int i = 0; i < sizes.size(); i++)
 if (sizes.get(i).width < size.width && sizes.get(i).width > 480)
 size = sizes.get(i);
 } else
 for (int i = 0; i < sizes.size(); i++)
 if (sizes.get(i).width > size.width && sizes.get(i).width < 1000)
 size = sizes.get(i);
 params.setPictureSize(size.width, size.height);
 params.setRotation(rotate);
 camera.setDisplayOrientation(90);
 camera.setParameters(params);
 this.camera.startPreview();
 Log.e("1111111","startCamera");
 } catch (Exception var2) {
 var2.printStackTrace();
 }

 }
}

SimpleCameraView:

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.Iterator;

import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;

public class SimpleScannerFragment extends Fragment {
 private ImageScanner scanner;
 private SimpleCameraView cameraView;
 private PackageManager packageManager;
 private Vibrator vibrator;
 private Handler configurationHandler = new Handler();
 private Handler autoFocusHandler = new Handler();
 private Runnable reconfigureRunnable = new SimpleScannerFragment.CustomConfigureRunnable();
 private Runnable runAutoFocus = new SimpleScannerFragment.CustomAutoFocusRunnable();
 private PreviewCallback previewCallback = new SimpleScannerFragment.CustomPreviewCallback();
 private AutoFocusCallback autoFocusCallback = new SimpleScannerFragment.CustomAutoFocusCallback();
 private ScannerListener scannerListener;

 public SimpleScannerFragment() {
 }

 public void setScannerListener(ScannerListener scannerListener) {
 this.scannerListener = scannerListener;
 }

 public SimpleCameraView getCamera() {
 return cameraView;
 }

 public void onActivityCreated(Bundle savedInstanceState) {
 super.onActivityCreated(savedInstanceState);
 this.vibrator = (Vibrator) this.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
 this.scanner = new ImageScanner();
 this.scanner.setConfig(0, 256, 3);
 this.scanner.setConfig(0, 257, 3);
 Log.e("1111111","create");
 }

 public void onPause() {
 super.onPause();

 try {
 this.cameraView.stopCamera();
 this.stopAutofocus();
 Log.e("1111111","onpause");
 } catch (Exception var2) {
 var2.printStackTrace();
 }

 }

 public void onResume() {
 super.onResume();

 try {
 this.configureCamera();
 Log.e("1111111","onresume");
 } catch (Exception var2) {
 var2.printStackTrace();
 }

 }

 public void stopAutofocus() {
 if (this.isHaveAutoFocus() && this.cameraView.getCamera() != null) {
 this.autoFocusHandler.removeCallbacks(this.runAutoFocus);
 this.cameraView.getCamera().cancelAutoFocus();
 Log.e("1111111","stopautofocus");
 }

 }

 private void startAutofocus() {
 if (this.isHaveAutoFocus()) {
 this.autoFocusHandler.postDelayed(this.runAutoFocus, 3000L);
 this.cameraView.getCamera().autoFocus(this.autoFocusCallback);
 Log.e("1111111","startautofocus");
 }

 }

 public void onConfigurationChanged(Configuration newConfig) {
 super.onConfigurationChanged(newConfig);
 this.configureCamera();
 Log.e("1111111","onconfigurationchanged");
 }

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 this.cameraView = new SimpleCameraView(inflater.getContext(), this.previewCallback);
 Log.e("1111111","createview");
 return this.cameraView;
 }

 private boolean isHaveAutoFocus() {
 if (this.packageManager == null) {
 this.packageManager = this.getActivity().getPackageManager();
 }

 return this.packageManager.hasSystemFeature("android.hardware.camera.autofocus");
 }

 private void configureCamera() {
 this.configurationHandler.postDelayed(this.reconfigureRunnable, 500L);
 Log.e("1111111","confCamera");
 }

 static {
 System.loadLibrary("iconv");
 }

 private class CustomPreviewCallback implements PreviewCallback {
 private long lastSnapshotTime;

 private CustomPreviewCallback() {
 }

 public void onPreviewFrame(byte[] data, Camera incomingCamera) {
 try {
 if (System.currentTimeMillis() > this.lastSnapshotTime) {
 this.lastSnapshotTime = System.currentTimeMillis() + 3500L;
 Camera.Parameters e = incomingCamera.getParameters();
 Size previewSize = e.getPreviewSize();
 Image barcode = new Image(previewSize.width, previewSize.height, "Y800");
 barcode.setData(data);
 if (SimpleScannerFragment.this.scanner.scanImage(barcode) != 0) {
 SymbolSet scannerResults = SimpleScannerFragment.this.scanner.getResults();
 if (SimpleScannerFragment.this.vibrator != null) {
 SimpleScannerFragment.this.vibrator.vibrate(300L);
 }

 Iterator i$ = scannerResults.iterator();

 while (i$.hasNext()) {
 Symbol symbol = (Symbol) i$.next();
 if (SimpleScannerFragment.this.scannerListener == null) {
 Toast.makeText(SimpleScannerFragment.this.getActivity(), symbol.getData(), Toast.LENGTH_LONG).show();
 } else {
 SimpleScannerFragment.this.scannerListener.onDataReceive(symbol.getData(), symbol.getType());
 }
 }
 }
 }
 } catch (Exception var9) {
 var9.printStackTrace();
 }

 }
 }

 private class CustomConfigureRunnable implements Runnable {
 private CustomConfigureRunnable() {
 }

 public void run() {
 try {
 boolean e = SimpleScannerFragment.this.cameraView.configureCamera(SimpleScannerFragment.this.getResources().getConfiguration());
 if (!e) {
 SimpleScannerFragment.this.configurationHandler.postDelayed(this, 500L);
 SimpleScannerFragment.this.cameraView.stopCamera();
 } else {
 SimpleScannerFragment.this.configurationHandler.removeCallbacks(this);
 SimpleScannerFragment.this.cameraView.startCamera();
 SimpleScannerFragment.this.startAutofocus();
 }
 } catch (Exception var2) {
 var2.printStackTrace();
 }

 }
 }

 private class CustomAutoFocusRunnable implements Runnable {
 private CustomAutoFocusRunnable() {
 }

 public void run() {
 try {
 if (SimpleScannerFragment.this.cameraView != null && SimpleScannerFragment.this.cameraView.getCamera() != null && SimpleScannerFragment.this.isHaveAutoFocus()) {
 SimpleScannerFragment.this.cameraView.getCamera().autoFocus(SimpleScannerFragment.this.autoFocusCallback);
 }
 } catch (Exception var2) {
 var2.printStackTrace();
 }

 }
 }

 private class CustomAutoFocusCallback implements AutoFocusCallback {
 private CustomAutoFocusCallback() {
 }

 public void onAutoFocus(boolean success, Camera camera) {
 SimpleScannerFragment.this.autoFocusHandler.postDelayed(SimpleScannerFragment.this.runAutoFocus, 3000L);
 }
 }
}

清单:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<uses-feature android:name="android.hardware.vibrate" android:required="false"/>
<uses-feature android:name="android.hardware.camera"
 android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="true" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
 android:name="android.hardware.camera.flash"
 android:required="true" />

查看:

<fragment
 android:id="@+id/scannerFragment"
 class="com.app.reclamavdom.app.SimpleScannerFragment"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true" />

堆栈跟踪:

    06-15 12:10:18.485 13037-13037/com.app.reclamavdom.app E/1111111: create
06-15 12:10:18.485 13037-13037/com.app.reclamavdom.app E/1111111: confCamera
06-15 12:10:18.486 13037-13037/com.app.reclamavdom.app E/1111111: onresume
06-15 12:10:18.571 13037-13122/com.app.reclamavdom.app D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                         [ 06-15 12:10:18.586 13037:13037 D/         ]
                                                                         HostConnection::get() New Host Connection established 0xacbb29a0, tid 13037


                                                                         [ 06-15 12:10:19.067 13037:13122 D/         ]
                                                                         HostConnection::get() New Host Connection established 0xaec26630, tid 13122
06-15 12:10:19.083 13037-13122/com.app.reclamavdom.app I/OpenGLRenderer: Initialized EGL, version 1.4
06-15 12:10:19.843 13037-13037/com.app.reclamavdom.app E/1111111: surfaceCreated
06-15 12:10:19.958 13037-13037/com.app.reclamavdom.app E/1111111: startCamera
06-15 12:10:19.958 13037-13037/com.app.reclamavdom.app E/1111111: surfaceChanged
06-15 12:10:19.990 13037-13037/com.app.reclamavdom.app I/Choreographer: Skipped 78 frames!  The application may be doing too much work on its main thread.
06-15 12:10:26.913 13037-13043/com.app.reclamavdom.app W/art: Suspending all threads took: 84.122ms
06-15 12:10:27.227 13037-13037/com.app.reclamavdom.app W/art: Verification of android.graphics.drawable.Drawable android.support.v7.widget.ActionMenuPresenter.getOverflowIcon() took 138.051ms
06-15 12:10:27.657 13037-13037/com.app.reclamavdom.app E/1111111: getCamera
06-15 12:10:27.658 13037-13037/com.app.reclamavdom.app E/1111111: configureCamera
06-15 12:10:27.671 13037-13037/com.app.reclamavdom.app E/1111111: startCamera
06-15 12:10:27.677 13037-13037/com.app.reclamavdom.app E/1111111: getCamera
06-15 12:10:27.677 13037-13037/com.app.reclamavdom.app E/1111111: startautofocus
06-15 12:10:27.696 13037-13037/com.app.reclamavdom.app I/Choreographer: Skipped 460 frames!  The application may be doing too much work on its main thread.
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err: java.lang.RuntimeException: getParameters failed (empty parameters)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at android.hardware.Camera.native_getParameters(Native Method)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at android.hardware.Camera.getParameters(Camera.java:1890)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at com.app.reclamavdom.app.SimpleScannerFragment$CustomPreviewCallback.onPreviewFrame(SimpleScannerFragment.java:144)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1110)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at android.os.Looper.loop(Looper.java:148)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
06-15 12:10:27.904 13037-13037/com.app.reclamavdom.app W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-15 12:10:27.905 13037-13037/com.app.reclamavdom.app E/Camera: Error 2
06-15 12:10:27.905 13037-13037/com.app.reclamavdom.app E/Camera: Error 2
06-15 12:10:30.708 13037-13037/com.app.reclamavdom.app E/1111111: getCamera
06-15 12:10:30.717 13037-13037/com.app.reclamavdom.app E/1111111: getCamera
06-15 12:10:30.717 13037-13037/com.app.reclamavdom.app E/Camera: Error 2
06-15 12:10:30.717 13037-13037/com.app.reclamavdom.app E/Camera: Error 2

4 个答案:

答案 0 :(得分:1)

在应用关闭时尝试正确关闭相机。让会话保持打开状态可能会导致Could not open camera: Too many users (-87)错误,该错误不会显示在您的应用标记下,因此通常会在日志中遗漏。

如果是这种情况,重启设备应该可以启动并运行

答案 1 :(得分:0)

getParameters()回调中收到的相机对象调用onPreviewFrame()是个坏主意。此对象可以为null,或者不处于适当的状态。即使这个功能成功,它也非常慢。因此,只需将您配置的预览大小保留为类的私有成员,并在每个预览框架中使用它们而不是getPreviewSize()

我还建议在预览循环之外创建Image barcode一次,并在回调内部仅创建setData()。您会惊讶地发现这种重复分配的速度有多慢。

答案 2 :(得分:0)

我想您两次打电话给相机。就我而言,我使用“ onCreate”和“ onResume”方法打开相机。

答案 3 :(得分:0)

Nitay回答正确。当我尝试打开相机(已打开)而不关闭相机时出现此错误。

已经打开相机拿着这个了!

cam.startPreview()

在访问其他摄像机或已打开的相机之前,应先关闭该相机!

cam.stopPreview()
cam.release()