Zxing框架进行二维码扫描时候会发现,随着分辨率的增加,扫描框会越来越小,SurfaceView扫描窗口就看不见了,我们可以自己定义扫描窗口的大小,以及适配屏幕问题。
Zxing包中有个类CameraManager,它是来设置扫描框的大小
扫描框框初始化数值
private static int MIN_FRAME_WIDTH = 240;
private static int MIN_FRAME_HEIGHT = 240;
private static int MAX_FRAME_WIDTH = 480;
private static int MAX_FRAME_HEIGHT = 360;
此类里面有个getFramingRect方法用来设置扫描的框的大小,如果要修改扫描框的大小可以在这个方法里修改
public Rect getFramingRect() {
Point screenResolution = configManager.getScreenResolution();
if (framingRect == null) {
if (camera == null) {
return null;
} MIN_FRAME_WIDTH = Dp2Px(context,180);
MIN_FRAME_HEIGHT =Dp2Px(context,180);
MAX_FRAME_WIDTH = Dp2Px(context,280);
MAX_FRAME_HEIGHT =Dp2Px(context,240); 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 = (screenResolution.x - width) / 2;
int topOffset = (screenResolution.y - height) / 2;
framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
Log.d(TAG, "Calculated framing rect: " + framingRect);
}
return framingRect;
}
要让扫描框适应不同的分辨率,我们需要根据分辨率将扫描框的初始值转化就好。
MIN_FRAME_WIDTH = Dp2Px(context,180);
MIN_FRAME_HEIGHT =Dp2Px(context,180) ;
MAX_FRAME_WIDTH = Dp2Px(context,280);
MAX_FRAME_HEIGHT =Dp2Px(context,240) ;
dp转为px的方法为
public static int Dp2Px( Context context,float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}