/*
* Copyright (C) 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zijunlin.Zxing.Demo.camera;
import java.io.IOException;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Build;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.SurfaceHolder;
/**
* This object wraps the Camera service object and expects to be the only one talking to it. The
* implementation encapsulates the steps needed to take preview-sized images, which are used for
* both preview and decoding.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class CameraManager {
private static final String TAG = CameraManager.class.getSimpleName();
private static final int MIN_FRAME_WIDTH = 240;
private static final int MIN_FRAME_HEIGHT = 240;
private static final int MAX_FRAME_WIDTH = 480;
private static final int MAX_FRAME_HEIGHT = 500;
private static CameraManager cameraManager;
static final int SDK_INT; // Later we can use Build.VERSION.SDK_INT
static {
int sdkInt;
try {
sdkInt = Integer.parseInt(Build.VERSION.SDK);
} catch (NumberFormatException nfe) {
// Just to be safe
sdkInt = 10000;
}
SDK_INT = sdkInt;
}
private final Context context;
private final CameraConfigurationManager configManager;
private Camera camera;
private Rect framingRect;
private Rect framingRectInPreview;
private boolean initialized;
private boolean previewing;
private final boolean useOneShotPreviewCallback;
/**
* Preview frames are delivered here, which we pass on to the registered handler. Make sure to
* clear the handler so it will only receive one message.
*/
private final PreviewCallback previewCallback;
/** Autofocus callbacks arrive here, and are dispatched to the Handler which requested them. */
private final AutoFocusCallback autoFocusCallback;
/**
* Initializes this static object with the Context of the calling Activity.
*
* @param context The Activity which wants to use the camera.
*/
public static void init(Context context) {
if (cameraManager == null) {
cameraManager = new CameraManager(context);
}
}
/**
* Gets the CameraManager singleton instance.
*
* @return A reference to the CameraManager singleton.
*/
public static CameraManager get() {
return cameraManager;
}
private CameraManager(Context context) {
this.context = context;
this.configManager = new CameraConfigurationManager(context);
// Camera.setOneShotPreviewCallback() has a race condition in Cupcake, so we use the older
// Camera.setPreviewCallback() on 1.5 and earlier. For Donut and later, we need to use
// the more efficient one shot callback, as the older one can swamp the system and cause it
// to run out of memory. We can't use SDK_INT because it was introduced in the Donut SDK.
//useOneShotPreviewCallback = Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.CUPCAKE;
useOneShotPreviewCallback = Integer.parseInt(Build.VERSION.SDK) > 3; // 3 = Cupcake
previewCallback = new PreviewCallback(configManager, useOneShotPreviewCallback);
autoFocusCallback = new AutoFocusCallback();
}
/**
* Opens the camera driver and initializes the hardware parameters.
*
* @param holder The surface object which the camera will draw preview frames into.
* @throws IOException Indicates the camera driver failed to open.
*/
public void openDriver(SurfaceHolder holder) throws IOException {
if (camera == null) {
camera = Camera.open();
if (camera == null) {
throw new IOException();
}
camera.setPreviewDisplay(holder);
if (!initialized) {
initialized = true;
configManager.initFromCameraParameters(camera);
}
configManager.setDesiredCameraParameters(camera);
// SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// if (prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false)) {
// FlashlightManager.enableFlashlight();
// }
}
}
/**
* Closes the camera driver if still in use.
*/
public void closeDriver() {
if (camera != null) {
FlashlightManager.disableFlashlight();
camera.release();
camera = null;
}
}
/**
* Asks the camera hardware to begin drawing preview frames to the screen.
*/
public void startPreview() {
if (camera != null && !previewing) {
camera.startPreview();
previewing = true;
}
}
/**
* Tells the camera to stop drawing preview frames.
*/
public void stopPreview() {
if (camera != null && previewing) {
if (!useOneShotPreviewCallback) {
camera.setPreviewCallback(null);
}
camera.stopPreview();
previewCallback.setHandler(null, 0);
autoFocusCallback.setHandler(null, 0);
previewing = false;
}
}
/**
* A single preview frame will be returned to the handler supplied. The data will arrive as byte[]
* in the message.obj field, with width and height encoded as message.arg1 and message.arg2,
* respectively.
*
* @param handler The handler to send the message to.
* @param message The what field of the message to be sent.
*/
public void requestPreviewFrame(Handler handler, int message) {
if (camera != null && previewing) {
previewCallback.setHandler(handler, message);
if (useOneShotPreviewCallback) {
camera.setOneShotPreviewCallback(previewCallback);
} else {
camera.setPreviewCallback(previewCallback);
}
}
}
/**
* Asks the camera hardware to perform an autofocus.
*
* @param handler The Handler to notify when the autofocus completes.
* @param message The message to deliver.
*/
public void requestAutoFocus(Handler handler, int message) {
if (camera != null && previewing) {
autoFocusCallback.setHandler(handler, message);
//Log.d(TAG, "Requesting auto-focus callback");
camera.autoFocus(autoFocusCallback);
}
}
/**
* Calculates the framing rect which the UI should draw to show the user where to place the
* barcode. This target helps with alignment as well as forces the user to hold the device
* far enough away to ensure the image will be in focus.
*
* @return The rectangle to draw on screen in window coordinates.
*/
public Rect getFramingRect() {
Point screenResolution = configManager.getScreenResolution();
if (framingRect == null) {
if (camera == null) {
return null;
}
int width = screenResolution.x * 3 / 4;
if (width < MIN_FRAME_WIDTH) {
width = MIN_FRAME_WIDTH;
} else if (width > MAX_FRAME_WIDTH) {
width = MAX_FRAME_WIDTH;
}
int height = screenResolution.y * 3 / 4;
if (height < MIN_FRAME_HEIGHT) {
height = MIN_FRAME_HEIGHT;
} else if (height > MAX_FRAME_HEIGHT) {
height = MAX_FRAME_HEIGHT;
}
int leftOffset = (screenRes
Android应用源码之android 实现竖屏二维码扫描.zip
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
在本压缩包“Android应用源码之android 实现竖屏二维码扫描.zip”中,我们可以深入探究如何在Android平台上实现一个竖屏模式下的二维码扫描功能。这个源码实例为Android开发者提供了一个宝贵的参考资料,有助于理解二维码扫描的核心技术以及如何将其实现到实际应用中。 我们来关注Android应用设计的基本流程。在Android应用开发中,通常会包含以下几个关键组件:Activity(界面)、布局文件(XML)、Java代码(业务逻辑)和资源文件(如图片、字符串等)。在这个实例中,主要涉及的是一个用于扫描二维码的Activity,它需要处理用户界面的显示、相机权限的请求以及扫描结果的处理。 1. **界面设计**: - 使用XML布局文件来定义扫描界面。通常会有一个全屏的SurfaceView用于显示相机预览,并可能包含一些额外的元素,如扫描框、闪光灯开关按钮等。 - 布局文件需要适配竖屏模式,确保在手机屏幕旋转时,扫描界面依然能够正确显示。 2. **权限管理**: - 扫描二维码需要访问摄像头的权限。在Android 6.0及以上版本,需要在运行时动态请求权限,因此在源码中会有相应的权限检查和请求代码。 3. **Camera API**: - Android的Camera API用于控制摄像头。开发者需要设置相机参数,如焦距、曝光等,然后通过SurfaceHolder回调函数来显示相机预览。 4. **二维码扫描库**: - 为了识别二维码,开发者通常会集成第三方库,如Zxing(ZXing是“Zebra Crossing”的缩写,意为斑马线)或Google的Mobile Vision API。这些库提供了二维码检测和解码的功能。 5. **扫描逻辑**: - 在扫描过程中,需要不断捕获相机帧并处理。当检测到二维码时,会触发回调函数,传递解码后的数据给应用进行处理。 6. **结果处理**: - 解码成功后,应用程序可以弹出提示框显示扫描结果,或者根据业务逻辑执行相应的操作,比如打开链接、保存数据等。 7. **性能优化**: - 为了提高扫描性能和用户体验,可能需要对相机预览的帧率、图像处理算法等进行优化,减少延迟和功耗。 8. **错误处理**: - 源码中应该包含了错误处理机制,如相机无法打开、权限被拒绝等情况的处理。 通过分析这个源码,开发者可以了解到Android应用如何与硬件交互、如何集成第三方库以及如何实现扫描功能。这对于初学者或有经验的开发者来说,都是一个很好的学习和实践案例。同时,源码中的注释和设计思路也会帮助理解整个扫描过程,进一步提升Android应用开发技能。
- 1
- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
- 粉丝: 2970
- 资源: 7735
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- springboot项目校运会管理系统.zip
- springboot项目校园志愿者管理系统.zip
- springboot项目新冠物资管理.zip
- 最新域名出售交易平台源码修复版
- springboot项目新冠物资管理系统的设计与实现.zip
- 多目标分布式光伏优化配置Matlab程序与详解 以改进微分进化算法为基础的优化模型,考虑分布式光伏投资及运维成本,网损以及电压稳定性指标为目标函数 程序解读详细,出15副左右的图
- springboot项目学生成绩分析和弱项辅助系统设计.zip
- springboot项目学生心理压力咨询评判.zip
- springboot项目学生信息管理系统论文__.zip
- 常用软件开发合同模版,史上最全模版
- springboot项目学校防疫物资管理平台的设计与实现boot.zip
- springboot项目学生选课系统.zip
- Deep learning深度学习的轴承故障诊断程序
- MATLAB代码:基于改进K-means算法的含电动汽车负荷源荷场景聚类 关键词:有序聚类 改进k-means聚类 电动汽车负荷聚类 风光场景聚类 场景削减 仿真平台:MATLAB 主要内容:代码
- springboot项目牙科就诊管理系统.zip
- springboot项目养老保险管理系统.zip