请帮忙理解什么意思:E/Camera: Error 2。
我的相机代码仅在 android 6.0 上不起作用,其他的都有效。此代码用于扫描二维码。 6.0 预览版不包括在内,但您可以包括 flash。
运行时权限处于 Activity 状态。
有时:W/System.err: java.lang.RuntimeException: getParameters 在 onPreviewFrame 中失败(空参数)。
简单相机 View :
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();
}
}
}
简单相机 View :
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);
}
}
}
list :
<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
最佳答案
尝试在您的应用关闭时正确关闭相机。让 session 保持打开状态可能会导致 Could not open camera: Too many users (-87) 错误,该错误不会出现在您的应用程序标签下,因此通常会在日志中遗漏。
如果是这种情况,重启设备应该可以启动并运行
关于android - 相机错误2 android 6.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37834961/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]
尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot