2022年android条码照相 .pdf
关于 Zxing 二维码扫描开灯的问题复制链接 最近看了一款我查查 ” 的应用,举个他的那个扫描界面做的蛮好的,还有一个开灯的效果;于是乎,自己就像尝试着做一个,一开始总是会出问题的,不过功夫不负有心人,最后总算被我搞出来了。因为是豌豆荚截的图,所以看不到扫描效果,大家可以下载源码看。开始正题。一开始的时候,我从新声明了一个Camera 对象,然后按照正常的逻辑打开camera.open() ,打开LED灯,关灯等等。但是这样会报错的:出现这样的问题的原因是:系统只有一个Camera 对象,我再重新声明一个Camera 对象的话,当然会报错,这是我犯错误比较严重的地方,果然还是应该先细读一下Zxing 的源码啊!解决方法:因为Zxing 扫描启动的时候已经初始化了一个Camera 对象了,所以没有必要再重新声明一个对象。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 30 页 - - - - - - - - - 这个初始方法是在CameraManager类里面,大家可以去看下。到了这里问题已经解决一半了,只要我们在CameraManager里面添加一个获取camera 对象的方法就行, 然后你在 CaptureActivity里面调用那个方法来获取已经打开的camara,最后设置一下开灯,关灯呢关灯一些相关属性就OK 了。开灯、关灯的关键代码,这个百度也能搜到的,记得要添加相关权限:/* 开灯*/private OnClickListener openListener = new OnClickListener() Overridepublic void onClick(View v) camera = CameraManager.getCamera();parameter = camera.getParameters();/ TODO 开灯if (isOpen) btnOpen.setText(关灯 );parameter .setFlashMode(Parameters.FLASH_MODE_TORCH);camera.setParameters(parameter); isOpen = false; else / 关灯btnOpen.setText(开灯 );parameter .setFlashMode(Parameters.FLASH_MODE_OFF);camera.setParameters(parameter);isOpen = true;名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 30 页 - - - - - - - - - 不过,程序写到这里还并没有结束,运行以后,发现可以开灯了,但是关灯以后还是会报错的。报什么错呢?看下面:大家一看应该知道是什么问题了,不知道的话百度一下吧:http:/ void surfaceDestroyed(SurfaceHolder holder) camera.setPreviewCallback(null) ;camera.stopPreview();camera.release();camera = null;我试了,可以关灯了,但是退出应用的时候还是报错了,我想是不是在OnDestory() 里面也加上这句话,结果运行还是不行。最后想了一想,我调用的不是CameraManager里面的初始化方法吗?然后再回过头来看这个类,发现里面已经写好了桌面销毁Camera 对象的方法了:/* Tells the camera to stop drawing preview frames.*/public static void stopPreview() if (camera != null & previewing) if (!useOneShotPreviewCallback) camera.setPreviewCallback(null);camera.stopPreview();previewCallback.setHandler(null, 0);autoFocusCallback.setHandler(null, 0);名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 30 页 - - - - - - - - - previewing = false;于是乎,我就在surfaceDestroyed里面调用了这个stopPreView() 这个方法,运行之后果断不报错了。到这里为止,给二维码扫描加开灯的功能就真的大功告成了。目前测试还没有发现扫描问题,如果以后大家发现了问题,希望能一起探讨。最后给大家分享一些关于Zxing 二维码扫描的资源,个人觉得写的真不错,里面还有一些延伸的知识,楼主真的很细心啊!地址: http:/ KB, 下载次数 : 4) Android 硬件 android camera 开发Method called after release()问题复制链接 现 在 在 做android相 机 开 发 , 遇 到 一 个 问 题 , 就 是 拍 照 完 , 去 预 览 , 可 是 这 样 几 次 以 后 就 报 异 常 了java.lang.RuntimeException: Method called after release()刚刚搜到一个说法是:说的是权限的问题不知道哪个大哥知道这个具体的原因大哥些都没有人知道么. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 30 页 - - - - - - - - - 我把 这个权限加上以后相机不是很卡,不知道为什么,那位大侠知道怎么解决这个问题。public void surfaceDestroyed(SurfaceHolder holder) camera.setPreviewCallback(null) ;camera.stopPreview();camera.release();camera = null;Android 打开关闭闪光灯 (里程碑 2.1) 不同的手机,开启闪光灯的方法不一样,这里以摩托罗拉里程碑的手机为例main.xml: Activity 代码:package com.android.flashlight; import android.app.Activity; import android.os.Bundle; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 30 页 - - - - - - - - - import android.view.View; import android.widget.Button; publicclass AndroidFlashLightActivity extends Activity /* Called when the activity is first created. */private Button mBtnOpen,mBtnClose; private MyFlashLight myFlashLight; Overridepublicvoid onCreate(Bundle savedInstanceState) super .onCreate(savedInstanceState); setContentView(R.layout.main); mBtnOpen = (Button) findViewById(R.id.open); mBtnClose = (Button) findViewById(R.id.close); try myFlashLight = new MyFlashLight(); catch (Exception e) e.printStackTrace(); mBtnOpen.setOnClickListener(new Button.OnClickListener() Overridepublicvoid onClick(View v) if (myFlashLight.isEnabled() = false) myFlashLight.enable(true ); ); mBtnClose.setOnClickListener(new Button.OnClickListener() Overridepublicvoid onClick(View v) if (myFlashLight.isEnabled() = true ) myFlashLight.enable(false); ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 30 页 - - - - - - - - - 封装的方法:封装的方法:package com.android.flashlight; import java.lang.reflect.Method; import android.os.IBinder; publicclass MyFlashLight private Object svc = null; private Method getFlashlightEnabled = null; private Method setFlashlightEnabled = null; SuppressWarnings( unchecked) public MyFlashLight() throws Exception try / call ServiceManager.getService(hardware) to get an IBinder for the service. / this appears to be totally undocumented and not exposed in the SDK whatsoever. Class sm = Class.forName(android.os.ServiceManager); Object hwBinder = sm.getMethod(getService, String.class ).invoke(null, hardware); / get the hardware service stub. this seems to just get us one step closer to the proxy Class hwsstub = Class.forName(android.os.IHardwareService$Stub); Method asInterface = hwsstub.getMethod(asInterface, android.os.IBinder.class ); svc = asInterface.invoke(null, (IBinder) hwBinder); / grab the class (android.os.IHardwareService$Stub$Proxy) so we can reflect on its methods Class proxy = svc.getClass(); / save methods getFlashlightEnabled = proxy.getMethod(getFlashlightEnabled); setFlashlightEnabled = proxy.getMethod(setFlashlightEnabled, boolean . class ); catch (Exception e) thrownew Exception(LED could not be initialized); publicboolean isEnabled() try return getFlashlightEnabled.invoke(svc).equals(true ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 30 页 - - - - - - - - - catch (Exception e) returnfalse; publicvoid enable(boolean tf) try setFlashlightEnabled.invoke(svc, tf); catch (Exception e) 其他一些手机开启闪光灯的方法可以参考http:/ 开启闪光灯的几种办法1、用 IHardwareService直接控制Android1.5 以前是直接提供这个接口的,之后呢,需要我们自己来做一下。在你的项目中新建一个包android.os, 新建一个 IHardwareService.aidl文件,内容如下:javaview plaincopy package android.os; /* hide */interface IHardwareService / obsolete flashlight supportboolean getFlashlightEnabled(); void setFlashlightEnabled(boolean on); 然后在你的程序中引入javaview plaincopy import android.os.IHardwareService; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 30 页 - - - - - - - - - /* * 设置闪光灯的开启和关闭 * param isEnable * author linc * date 2012-3-18 */privatevoid setFlashlightEnabled(boolean isEnable) try Method method = Class.forName(android.os.ServiceManager).getMethod(getService, String.class); IBinder binder = (IBinder) method.invoke(null, new Object hardware ); IHardwareService localhardwareservice = IHardwareService.Stub.asInterface(binder); localhardwareservice.setFlashlightEnabled(isEnable); catch (Exception e) e.printStackTrace(); 这样就可以开启闪光灯了。要使用的权限:htmlview plaincopy 2、用照相机来控制闪光灯这是思维的另一个方向。 Camera 自带丰富的接口与Parameters ,我们只管拿来使用就好了。javaview plaincopy private Camera camera = null; private Parameters parameters = null; / 直接开启camera = Camera.open(); parameters = camera.getParameters(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 30 页 - - - - - - - - - parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);/ 开启 camera.setParameters(parameters); / 直接关闭parameters.setFlashMode(Parameters.FLASH_MODE_OFF);/ 关闭 camera.setParameters(parameters); camera.release(); 如果这样遇到问题的话,可以尝试用camera 的 preview 方法:javaview plaincopy privatevoid OpenLightOn() if ( null = m_Camera ) m_Camera = Camera.open(); Camera.Parameters parameters = m_Camera.getParameters(); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); m_Camera.setParameters( parameters ); m_Camera.autoFocus( new Camera.AutoFocusCallback () publicvoid onAutoFocus(boolean success, Camera camera) ); m_Camera.startPreview(); privatevoid CloseLightOff() if ( m_Camera != null ) m_Camera.stopPreview(); m_Camera.release(); m_Camera = null; Android 调用系统 Camera 程序照相,获取照片下面是我自己整理的源码,网络上好多不能够运行,或者有bug 。我在 emulator android 2.1运行良好,源码注释一定程度能够自我解释强烈推荐配合adb locat Take2:d *:s查看程序运行函数调用情况对 Activity生命周期不是很理解的,请先看我之前的一片文章http:/ 代码名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 30 页 - - - - - - - - - Xml 代码名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 30 页 - - - - - - - - - Java 代码package com.busclient; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 30 页 - - - - - - - - - import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; publicclass Take2 extends Activity implements SurfaceHolder.Callback privatefinalstatic String TAG = Take2 ; private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; private Camera mCam; privateboolean hasStartPreview = false ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 30 页 - - - - - - - - - private Button btnTakePicture; / Camera API:/ Call release() to release the camera for use by other applications./ Applications should release the camera immediately in onPause() (and/ re-open() it in onResume().Overrideprotectedvoid onCreate(Bundle savedInstanceState) super .onCreate(savedInstanceState); setContentView(R.layout.takephoto); Log.d(TAG, onCreate); / 取得 surfaceView的引用 ,surface在 surfaceView中展现/ 当 surfaceView可见时, surface会被创建,实现surfaceCreated进行特定操作surfaceView = (SurfaceView) findViewById(R.id.surface_camera); btnT akePicture = (Button) findViewById(R.id.cameraButton); / 注册按钮实现拍照btnT akePicture.setOnClickListener(new OnClickListener() Override名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 30 页 - - - - - - - - - publicvoid onClick(View v) if (mCam != null ) / 调用 mCam进行拍照/ param1 Camera.ShutterCallback / param2 Camera.PictureCallback raw 当原始图片数据获得时,会执行PictureCallback/ param3 Camera.PictureCallback compressed 当压缩数据获得时,会执行PictureCallbackmCam.takePicture(null , null , pictureCallBack); ); / 要操作 surface ,只有通过surfaceHoldersurfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this ); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); private PictureCallback pictureCallBack = new Camera.PictureCallback() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 30 页 - - - - - - - - - Overridepublicvoid onPictureTaken(byte data, Camera camera) if (mCam != null ) if (data != null ) File f = new File( /data/data/com.busclient/picture1.jpg); FileOutputStream fout = null ; try if (!f.exists() f.createNewFile(); fout = new FileOutputStream(f); fout.write(data); fout.close(); Log.d(TAG, write success); Thread.sleep(1500 ); catch (IOException e) e.printStackTrace(); catch (InterruptedException e) e.printStackTrace(); finally try if (fout != null ) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 30 页 - - - - - - - - - fout.close(); fout = null ; catch (Exception e) e.printStackTrace(); finish(); ; / surface只能够由一个线程操作,一旦被操作,其他线程就无法操作surfaceOverridepublicvoid surfaceCreated(SurfaceHolder holder) Log.d(TAG, surfaceCreated); try / 必须设置一个初始化的surfaceHolder,若没有surface (由 holder操作 surface ) ,/ 则 camera无法启动预览(就是一般打开照相机屏幕能够动态显示场景?描述的不够好)mCam.setPreviewDisplay(surfaceHolder); catch (IOException e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 30 页 - - - - - - - - - Log.d(TAG, error ); mCam.release(); mCam = null ; / 在 surfaceCreated后调用,当surface发生变化也会触发该方法,这个方法/ 一般至少被调用一次Overridepublicvoid surfaceChanged(SurfaceHolder holder, int format, int width, int height) Log.d(TAG, surfaceChanged); / 调用 startPreview使预览 surface可以更新,拍照/ 必须启动预览,而startPreview必须在 setPreviewDisplay(surfaceHolder)之后if (mCam != null & hasStartPreview = false ) mCam.startPreview(); hasStartPreview = true ; / 析构 surface名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 30 页 - - - - - - - - - Overridepublicvoid surfaceDestroyed(SurfaceHolder holder) Log.d(TAG, surfaceDestroyed); / 活都被 onPause抢去了 Overrideprotectedvoid onDestroy() Log.d(TAG, onDestroy); super .onDestroy(); / onPause比 surfaceDestroyed() 先调用Overrideprotectedvoid onPause() Log.d(TAG, onPause); if (hasStartPreview) mCam.stopPreview(); mCam.release(); mCam = null ; hasStartPreview = false ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 19 页,共 30 页 - - - - - - - - - super .onPause(); Overrideprotectedvoid onRestart() Log.d(TAG, onRestart); super .onRestart(); Overrideprotectedvoid onResume() Log.d(TAG, onResume); if (mCam = null ) mCam = Camera.open(); super .onResume(); Overrideprotectedvoid onStart() Log.d(TAG, onStart); super .onStart(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 20 页,共 30 页 - - - - - - - - - Overrideprotectedvoid onStop() Log.d(TAG, onStop); super .onStop(); android 拍照功能,可连拍哦 复制链接 上一个项目由于客户要拍照功能。为了避免兼容问题,先使用的是系统的相机,但是发现一个问题:正常打开系统的相机程序是可以进行连续拍照的,可是通过android.media.action.IMAGE_CAPTURE 调用后,只能拍完一张确定,然后就退出了。不知道是为什么?(如果有哪位大神知道,请赐教哈)后来,就想办法自己做一个吧。参考了http:/ (420.63 KB, 下载次数 : 168) Zxing 不依赖于安装BarcodeScanner实现有两个办法。1. 你自己简化ZXing 中自带的那个android client 2. 如果不想自己去简化(有点烦的 ),那么就严格按照下面的说法去做:Edit: Sean Owen, one of the developers for ZXing has posted a comment to this blog warning of the pitfalls of integrating ZXing into your own app; doing so just to avoid having your users take that extra step of installing from the market is not a good reason. I completely agree with this. There are many advantages to using the intent based approach as outlined in his comments. My motivation is for an enterprise app that does not have access to the Android market and involves my client installing zxing manually on thousands of devices before they are able to distribute to its business customers. So to be clear, do not use this method unless it is absolutely necessary, and if you do have to make sure that override your intent filters so that other apps that want to use zxing do not end up calling your modified version. Also, if zxing is already 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 21 页,共 30 页 - - - - - - - - - installed, then you should use that by default instead of your modified version. ZXing is one of the most popular barcode scanning applications on the market. They make it very easy for you to integrate into your application via an intent but this means that your users must manually install the application from the market. Fortunately, the app is also open source so I will show you how to cleanly build this capability into your project. Please note that the awesome developers of this product have released the src under the Apache v2.o license so please be sure to adhere to the terms of this license and give them full credit for their work. http:/www.apache.org/licenses/LICENSE-2.0 Step One: Obtain the zxing src code The src can be found at http:/ Specifically you only need the android/ and the core/ projects. Use svn to checkout these to your local hard-drive. Step Two: Build zxing c