- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
请帮忙理解什么意思: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/
有人能给我一些代码示例,了解如何从相机捕获“完整”图像,然后在“startActivityForResult”中将其转换为字节,以及显示在 imageView 中的位图。任何帮助将不胜感激。 山姆 最
我有一个带有两个圆的组,当我用平移过渡移动其中一个时,我应该看到静止的一个保持在中心(位于场景图的中间),而另一个移动。取而代之的是,“摄像机”跟随移动的圆圈,使其看起来好像都在分开。 有没有一种方法
我希望停止 Phonegap 将图片保存到 iOS 上的相机胶卷。 我发现了一种可能的解决方案,但我不太喜欢它。该解决方案需要通过删除这行代码来 fork phonegap API。 UIImageW
我最近开始使用 JOGL,我知道如何在 Canvas 上创建和绘制对象,但我找不到有关如何设置和旋转相机的教程或说明。我只找到了源代码,但由于我对此很陌生,所以它没有太大帮助。 有人知道一个好的教程或
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我想实现这样的目标: 因此,您捕获和处理的唯一内容是矩形中的区域。我需要这个,因为我想做一些 OCR 而我不想要整个屏幕。 最佳答案 也许可以帮助您这个来源,(在相机预览顶部绘制一个边界框以捕获部分图
我正在开发一个 Android 应用程序,我希望我的应用程序能够拍照并显示它们。我的问题是它制作了图片,将它存储在 SD 卡上但没有在我的应用程序中显示它,ImageView 保持空白。这是我的代码:
我正在研究 Android 相机教程,SDK 11。出于某种原因,我在 handleCameraPhoto() 中得到了一个空指针。我唯一看到的是“无法将结果 ResultInfo{who=null,
我一直在尝试实现此代码,我在其中从相机捕获图像并将其显示在我的图像查看器中,然后可以将其设置为墙纸。它在我的模拟器中运行良好,但在我的手机上,当我拍摄图像并单击“确定”时,它会强制关闭。有什么帮助吗?
我想将预览中的某些像素更改为比其余像素暗一点。假设我希望预览的上半部分比预览的下半部分暗一点。 我试图像这样使用 setPreviewCallback: camera.setPreviewCallba
我想为我的应用程序启用相机/照片库设置。我可以显示警报以编程方式启用这些吗就像应用程序启动时一样。我已附上图片以显示我希望看到我的应用程序的类似设置 对于我的应用程序,我没有看到此设置。 我的问题是这
所以我正在开发类似于相机应用程序的东西。它在原生 OpenGL 纹理上绘制实时相机预览。如果按下按钮,则会以原始形式捕获静态图像(可能比实时预览分辨率更高)。 这是我的代码的大纲。为了清楚起见,我没有
我想在我的 Android 应用程序中启动相机 Activity ,我知道该怎么做。我想问一下当相机 Activity 结束时,我如何检查它是否是用户拍摄的照片或视频? 已更新 我有一个对话框,其中询
我在横向模式下有自定义全屏摄像头。设备尺寸为 1024 x 600。 支持的预览尺寸列表 宽 x 高 176 x 144 320 x 240 352 x 288 528 x 432 640 x 480
请看下图!! 所以在这张图片中...您可以看到底部的弹出窗口,当用户“点击”“相机”按钮时,它会为用户提供 3 个选项。 那是什么观点?它是一个模态视图 Controller ,在选择一个选项时被关闭
我发布了一个应用程序,其中一项基本功能是允许用户拍照,然后将该照片保存在其外部存储上的特定文件夹中。 一切似乎都运行良好,但我现在收到两份报告,声称在拍照后单击“完成”退出相机(并返回到 Activi
我在尝试实时更换相机时遇到问题,它适用于本地视频,但远程人员看不到新相机,但仍然可以看到旧相机。我试图停止流并再次初始化,但仍然无法正常工作。这只是我的一些代码。 我到处搜索,但找不到解决方案。有人可
一个简单但非常复杂的问题:为 Flutter 相机添加点击对焦功能的最佳方法是什么? 我在整个万维网上搜索了优雅的解决方案,但一无所获。 你有想法吗? 最佳答案 我可能会迟到,但你可以试试 adv_c
我正在尝试使用 gluLookAt 方法设置 3D 相机。所以我有一个 10x10x10 的立方体,现在我想在那个立方体内移动相机。我有这样的事情: gluLookAt( camera->x,came
我赠送一个 UIImagePickerController在我的应用程序中通过在 sheet 中呈现逻辑修饰符。简而言之,以下三种类型处理显示和关闭 UIImagePickerController 的
我是一名优秀的程序员,十分优秀!