java ZXing Barcode Reader:如何在捕获屏幕周围制作自定义边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5852059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:07:27  来源:igfitidea点击:

ZXing Barcode Reader: How to make custom border around capture screen?

javaandroiduser-interfacezxing

提问by Androider

I want to put custom border around zxing capture screen (camera screen). What modification would I need to make for this? Which activity and layouts would I need to change to have this effect?

我想在 zxing 捕获屏幕(相机屏幕)周围放置自定义边框。我需要为此做哪些修改?我需要更改哪些活动和布局才能产生这种效果?

回答by inazaruk

You don't need to edit layouts at all.

您根本不需要编辑布局。

In ViewfinderViewfind onDrawmethod. It's the core that draws the "scanning rectangle". You can modify it the way you want.

ViewfinderView查找onDraw方法中。它是绘制“扫描矩形”的核心。您可以按照自己的方式修改它。

The code that actually draws the rectangle can be found here:

实际绘制矩形的代码可以在这里找到:

// Draw the exterior (i.e. outside the framing rect) darkened
paint.setColor(resultBitmap != null ? resultColor : maskColor);
canvas.drawRect(0, 0, width, frame.top, paint);
canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
canvas.drawRect(0, frame.bottom + 1, width, height, paint);

回答by ahodder

Hereis how a few others on SO did it.

以下是 SO 上的其他一些人是如何做到的。

Also look here, it looked useful.

也看看这里,它看起来很有用。

Finally, I would use thisone.

最后,我会用这个

回答by tej shah

Actually you can override the color in your own colors.xml file i.e.

其实你可以覆盖你自己的colors.xml文件中的颜色,即

<color name="viewfinder_border">#00d1cf</color>

回答by chathura

This question already has an answer. But if someone needs how to draw a border around capture screen, here is the code. inazaruk's answer is correct. My answer is just an extension for that.

这个问题已经有了答案。但是如果有人需要如何在捕获屏幕周围绘制边框,这里是代码。inazaruk 的回答是正确的。我的回答只是对此的扩展。

 //initialize new paint in the constructor
 Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 borderPaint.setColor(ContextCompat.getColor(context, R.color.colorPrimary));

 //inside onDraw
 int distance = (frame.bottom - frame.top) / 4;
 int thickness = 15;

 //top left corner
 canvas.drawRect(frame.left - thickness, frame.top - thickness, distance + frame.left, frame.top, borderPaint);
 canvas.drawRect(frame.left - thickness, frame.top, frame.left, distance + frame.top, borderPaint);

 //top right corner
 canvas.drawRect(frame.right - distance, frame.top - thickness, frame.right + thickness, frame.top, borderPaint);
 canvas.drawRect(frame.right, frame.top, frame.right + thickness, distance + frame.top, borderPaint);

 //bottom left corner
 canvas.drawRect(frame.left - thickness, frame.bottom, distance + frame.left, frame.bottom + thickness, borderPaint);
 canvas.drawRect(frame.left - thickness, frame.bottom - distance, frame.left, frame.bottom, borderPaint);

 //bottom right corner
 canvas.drawRect(frame.right - distance, frame.bottom, frame.right + thickness, frame.bottom + thickness, borderPaint);
 canvas.drawRect(frame.right, frame.bottom - distance, frame.right + thickness, frame.bottom, borderPaint);

enter image description here

在此处输入图片说明