web-dev-qa-db-ja.com

カメラエラー2 Android 6.0

意味を理解するのを手伝ってください:E /カメラ:エラー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
8
Faust

アプリが閉じたら、カメラを正しく閉じてみてください。セッションを開いたままにしておくと、Could not open camera: Too many users (-87)エラーが発生する可能性があります。このエラーは、アプリタグの下に表示されないため、通常はログに記録されません。

その場合は、デバイスを再起動すると起動して実行できるようになります

1
Nitay

Nitayは正解しました。カメラを閉じずに(すでに開いている)カメラを開こうとすると、このエラーが発生しました。

これを持ってもう開いたカメラ!

cam.startPreview()

他のキャメアまたはすでに開いているカメラにアクセスする前に、これを閉じる必要があります!

cam.stopPreview()
cam.release()
0
SagitSri

カメラを2回呼ぶと思います。私の場合、「onCreate」メソッドと「onResume」メソッドでカメラを開きます。

0
AlexS

getParameters()コールバックで受信したカメラオブジェクトに対してonPreviewFrame()を呼び出すことはお勧めできません。このオブジェクトはnullであるか、適切な状態にない可能性があります。そして、この機能が成功したとしても、それは非常に遅いです。したがって、クラスのプライベートメンバーとして構成したプレビューサイズを保持し、すべてのプレビューフレームでgetPreviewSize()の代わりにそれらを使用するだけです。

また、プレビューループの外側で、コールバック内でsetData()のみを設定してImage barcodeを1回作成することをお勧めします。このような定期的な割り当てがいかに遅いかに驚かれることでしょう。

0
Alex Cohn