Zxing二维码自定义修改关键代码

3.CameraManager类的getFramingRectInPreview()方法中将以下代码替换:

rect.left = rect.left * cameraResolution.x / screenResolution.x;

rect.right = rect.right * cameraResolution.x / screenResolution.x;

rect.top = rect.top * cameraResolution.y / screenResolution.y;

rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;

替换为

rect.left = rect.left * cameraResolution.y / screenResolution.x;

rect.right = rect.right * cameraResolution.y / screenResolution.x;

rect.top = rect.top * cameraResolution.x / screenResolution.y;

rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;

4.DecodeHandler类的decode方法中在activity.getCameraManager().buildLuminanceSource()之前添加以下代码:

byte[] rotatedData = new byte[data.length];

for(int y = 0;y < height; y++) {

for(int x = 0; x < width; x++)

rotatedData[x * height + height - y - 1] = data[x + y * width];

}

int tmp = width;

width = height;

height = tmp;

data = rotatedData;

5.关键的一步,解决竖屏后图像拉伸问题。CameraConfigurationManager类的initFromCameraParameters()方法中:

在Log.i(TAG, "Screen resolution: " + screenResolution);之后添加以下代码:

Point screenResolutionForCamera = new Point();

screenResolutionForCamera.x = screenResolution.x;

screenResolutionForCamera.y = screenResolution.y;

if(screenResolution.x < screenResolution.y) {

screenResolutionForCamera.x = screenResolution.y;

screenResolutionForCamera.y = screenResolution.x;

}

同时修改下一句为cameraResolution = findBestPreviewSizeValue(parameters,screenResolutionForCamera);

此外manifest中别忘了设置android:screenOrientation=“portrait”,至此竖屏修改完毕。

第四步:扫描框位置和大小修改

此时的扫描框是竖直拉伸的矩形,很难看,我们可以将其修改为正方形或扁平型的。

int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);

int height = findDesiredDimensionInRange(screenResolution.y,MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);

替换为

DisplayMetrics metrics = context.getResources().getDisplayMetrics();

int width = (int)(metrics.widthPixels * 0.6);

int height = (int) (width * 0.9);

此处我们根据屏幕分辨率来定扫描框大小更灵活一点,同时将偏移量topOffset修改为(screenResolution.y - height)/4

第五步:扫描框四个角和扫描线条修改

示例代码中的线条是居中且不动的,我们可以将其修改为上下移动的扫描线,且可以改变线条的样式。

在自定义扫描布局ViewfinderView类中的onDraw()方法中绘制四个角,关键代码如下:

//画出四个角

paint.setColor(getResources().getColor(R.color.green));

//左上角

canvas.drawRect(frame.left,frame.top, frame.left + 15,frame.top+ 5,paint);

canvas.drawRect(frame.left,frame.top, frame.left + 5,frame.top + 15,paint);

//右上角

canvas.drawRect(frame.right- 15,frame.top, frame.right,frame.top + 5,paint);

canvas.drawRect(frame.right- 5,frame.top, frame.right,frame.top + 15,paint);

//左下角

canvas.drawRect(frame.left,frame.bottom - 5,frame.left + 15,frame.bottom,paint);

canvas.drawRect(frame.left,frame.bottom - 15,frame.left + 5,frame.bottom,paint);

//右下角

canvas.drawRect(frame.right- 15,frame.bottom - 5,frame.right,frame.bottom, paint);

canvas.drawRect(frame.right- 5,frame.bottom - 15,frame.right,frame.bottom, paint);

此外将扫描线条修改为上下扫描的线,关键代码如下:

if((i += 5)< frame.bottom - frame.top) {

/* 以下为用渐变线条作为扫描线 */

//渐变图为矩形

//mDrawable.setShape(GradientDrawable.RECTANGLE);

//渐变图为线型

//mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);

//线型矩形的四个圆角半径

mDrawable.setCornerRadii(new float[] { 8, 8, 8, 8, 8, 8, 8, 8 });

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值