前期有优化过zxing扫描,是在之前别人整合进来的基础上做的优化,这里就记录下,方便以后查看.在整合进入后结构如下:
1:调整扫码框大小
在CameraManager类中
private static final intMIN_FRAME_WIDTH=960;
private static final intMIN_FRAME_HEIGHT=720;
private static final intMAX_FRAME_WIDTH=1200;// = 5/8 * 1920
private static final intMAX_FRAME_HEIGHT=675;// = 5/8 * 1080
设置宽高,如果需要设置成为方形框可以在CameraManager类的getFramingRect方法中设置:
height = width;
代码:
public synchronizedRect getFramingRect()
{
if(framingRect==null)
{
if(camera==null)
{
return null;
}
Point screenResolution =configManager.getScreenResolution();
if(screenResolution ==null)
{
// Called early, before init even finished
return null;
}
intwidth =findDesiredDimensionInRange(screenResolution.x,MIN_FRAME_WIDTH,MAX_FRAME_WIDTH);
intheight =findDesiredDimensionInRange(screenResolution.y,MIN_FRAME_HEIGHT,MAX_FRAME_HEIGHT);
if(!isrect)
{
//方形
height = width;
}
height = (int) (height);
intleftOffset = (screenResolution.x- width) /2;
// int topOffset = (screenResolution.y - height) / 2 ;
//距离上边为 51px,比actionBar(48px)高3px.不考虑staturbar高度
floatdpToPx = ScreenUtils.dpToPx(context,69);
inttopOffset = (int) dpToPx;
framingRect=newRect(leftOffset, topOffset, leftOffset + width, topOffset + height);
Log.d(TAG,"Calculated framing rect: "+framingRect);
}
returnframingRect;
}
2:调整扫码框显示距离(放大显示还是缩小显示)
在CameraConfigurationManager类中的setDesiredCameraParameters();
获取到camera对象的parameters属性,然后设置最大缩放
parameters.setZoom(camera.getCamera().getParameters().getMaxZoom());//只需要加这句
完成前后代码如下:
Camera theCamera = camera.getCamera();
Camera.Parameters parameters = theCamera.getParameters();
//parameters.setZoom(camera.getCamera().getParameters().getMaxZoom());
if(parameters ==null)
{
Log.w(TAG,"Device error: no camera parameters are available. Proceeding without configuration.");
return;
}
3:调整小米4c和魅族MX3后置拍照成像翻转问题
在CameraConfigurationManager类中的setDesiredCameraParameters()中添加如下代码:
完整代码如下:
voidsetDesiredCameraParameters(OpenCamera camera,booleansafeMode)
{
// See android.hardware.Camera.setCameraDisplayOrientation for
// documentation.
//===============================================
Camera.CameraInfo info =newCamera.CameraInfo();
Camera.getCameraInfo(0, info);
intdegrees =getDisplayRotation((Activity)context);
intresult;
if(info.facing== Camera.CameraInfo.CAMERA_FACING_FRONT)
{
result = (info.orientation+ degrees) %360;
result = (360- result) %360;// compensate the mirror
}
else
{// back-facing
result = (info.orientation- degrees +360) %360;
}
camera.getCamera().setDisplayOrientation(result);
//================================================
Camera theCamera = camera.getCamera();
Camera.Parameters parameters = theCamera.getParameters();
public static intgetDisplayRotation(Activity activity)
{
introtation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
switch(rotation)
{
caseSurface.ROTATION_0:
return0;
caseSurface.ROTATION_90:
return90;
caseSurface.ROTATION_180:
return180;
caseSurface.ROTATION_270:
return270;
}
return0;
}
4:设置扫码时候自动拉近聚焦
preview.setOnTouchListener(newView.OnTouchListener()
{
@Override
public booleanonTouch(View view, MotionEvent motionEvent)
{
focusOnTouch((int) motionEvent.getX(), (int) motionEvent.getY());
return false;
private voidfocusOnTouch(intx,inty)
{
Rect rect =newRect(x -100, y -100, x +100, y +100);
intleft = rect.left*2000/preview.getWidth() -1000;
inttop = rect.top*2000/preview.getHeight() -1000;
intright = rect.right*2000/preview.getWidth() -1000;
intbottom = rect.bottom*2000/preview.getHeight() -1000;
// 如果超出了(-1000,1000)到(1000, 1000)的范围,则会导致相机崩溃
left = left < -1000? -1000: left;
top = top < -1000? -1000: top;
right = right >1000?1000: right;
bottom = bottom >1000?1000: bottom;
focusOnRect(newRect(left, top, right, bottom));
}
protected voidfocusOnRect(Rect rect)
{
if(mCamera!=null)
{
Camera.Parameters parameters =mCamera.getParameters();// 先获取当前相机的参数配置对象
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);// 设置聚焦模式
Log.d(TAG,"parameters.getMaxNumFocusAreas() : "+ parameters.getMaxNumFocusAreas());
if(parameters.getMaxNumFocusAreas() >0)
{
List focusAreas =newArrayList();
focusAreas.add(newCamera.Area(rect,1000));
parameters.setFocusAreas(focusAreas);
}
mCamera.cancelAutoFocus();// 先要取消掉进程中所有的聚焦功能
mCamera.setParameters(parameters);// 一定要记得把相应参数设置给相机
mCamera.autoFocus(newCamera.AutoFocusCallback()
{
@Override
public voidonAutoFocus(booleansuccess,Cameracamera)
{
}
});
}
5:设置界面样式
略.
6:6.0以下手机禁止拍照后显示zxing弹框问题.
在6.0以下判断手机是否开启了相机权限,需要openCamera,没有权限则直接调整到权限开启界面.
/**
* 返回true 表示可以使用 返回false表示不可以使用
*/
public booleancameraIsCanUse()
{
booleanisCanUse =true;
Camera mCamera =null;
try
{
mCamera = Camera.open();
Camera.Parameters mParameters = mCamera.getParameters();//针对魅族手机
mCamera.setParameters(mParameters);
}
catch(Exception e)
{
isCanUse =false;
}
if(mCamera !=null)
{
try
{
mCamera.release();
}
catch(Exception e)
{
e.printStackTrace();
returnisCanUse;
}
}
returnisCanUse;
}