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

发布于 2024-11-04 19:08:17 字数 69 浏览 3 评论 0原文

我想在 zxing 捕获屏幕(相机屏幕)周围放置自定义边框。我需要为此进行什么修改?我需要更改哪些活动和布局才能达到此效果?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

杯别 2024-11-11 19:08:17

您根本不需要编辑布局。

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

实际绘制矩形的代码可以在 此处

// 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);

You don't need to edit layouts at all.

In ViewfinderView find onDraw method. It's the core that draws the "scanning rectangle". You can modify it the way you want.

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);
青衫儰鉨ミ守葔 2024-11-11 19:08:17

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

 //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);

输入图像描述这里

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.

 //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

七度光 2024-11-11 19:08:17

这里是其他人的做法。

另请参阅此处,它看起来很有用。

最后,我会使用 这个 一个。

Here is how a few others on SO did it.

Also look here, it looked useful.

Finally, I would use this one.

绳情 2024-11-11 19:08:17

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

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

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

<color name="viewfinder_border">#00d1cf</color>
临走之时 2024-11-11 19:08:17
public class CustomViewFinderView extends ViewFinderView {
        public CustomViewFinderView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setSquareViewFinder(true);
            setBorderColor(android.R.color.transparent);
            setMaskColor(android.R.color.transparent);
            setLaserColor(android.R.color.transparent);
        }
    }
@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mImageViewOpenAlbum = rootview.findViewById(R.id.imv_OpenAlbumScanCamera);
        RequestOptions options = new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(10)); // size of the corner radius
        Glide.with(getContext())
                .load(dataPath)
                .apply(options)
                .into(mImageViewOpenAlbum);
        contentFrame = (ViewGroup) rootview.findViewById(R.id.content_frame);

        mImageViewFlash = rootview.findViewById(R.id.imv_FlashScanCamera);
        mImageViewQuestion = rootview.findViewById(R.id.imv_QuestionScanCamera);
        mImageViewOpenAlbum = rootview.findViewById(R.id.imv_OpenAlbumScanCamera);
        Uri allVideosUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        sharedPreferences = getActivity().getSharedPreferences(KEY.Shared_Preferences_Settings, Context.MODE_PRIVATE);

        String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.BUCKET_ID};
        Cursor cursor = getContext().getContentResolver().query(allVideosUri, projection, null, null, null);

        try {
            cursor.moveToLast();
            do {
                dataPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            } while (dataPath.length() < 0);

            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Glide.with(Objects.requireNonNull(getContext()))
                .load(dataPath)
                .apply(options)
                .into(mImageViewOpenAlbum);

        mImageViewQuestion.setOnClickListener(this);
        mImageViewFlash.setOnClickListener(this);
        mImageViewOpenAlbum.setOnClickListener(this);

        mScannerView = new ZXingScannerView(getContext()) {
            @Override
            protected IViewFinder createViewFinderView(Context context) {
                return new CustomViewFinderView(context, null);
            }
        };
        contentFrame.addView(mScannerView);

// Здесь мы добавляем собственную рамку
        ImageView customFrame = new ImageView(getContext());
        customFrame.setImageResource(R.drawable.ic_frame);
        customFrame.setScaleX(0.5f);    // уменьшить размер рамки на 50% по оси X
        customFrame.setScaleY(0.5f);    // уменьшить размер рамки на 50% по оси Y
        contentFrame.addView(customFrame);



    }
public class CustomViewFinderView extends ViewFinderView {
        public CustomViewFinderView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setSquareViewFinder(true);
            setBorderColor(android.R.color.transparent);
            setMaskColor(android.R.color.transparent);
            setLaserColor(android.R.color.transparent);
        }
    }
@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mImageViewOpenAlbum = rootview.findViewById(R.id.imv_OpenAlbumScanCamera);
        RequestOptions options = new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(10)); // size of the corner radius
        Glide.with(getContext())
                .load(dataPath)
                .apply(options)
                .into(mImageViewOpenAlbum);
        contentFrame = (ViewGroup) rootview.findViewById(R.id.content_frame);

        mImageViewFlash = rootview.findViewById(R.id.imv_FlashScanCamera);
        mImageViewQuestion = rootview.findViewById(R.id.imv_QuestionScanCamera);
        mImageViewOpenAlbum = rootview.findViewById(R.id.imv_OpenAlbumScanCamera);
        Uri allVideosUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        sharedPreferences = getActivity().getSharedPreferences(KEY.Shared_Preferences_Settings, Context.MODE_PRIVATE);

        String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.BUCKET_ID};
        Cursor cursor = getContext().getContentResolver().query(allVideosUri, projection, null, null, null);

        try {
            cursor.moveToLast();
            do {
                dataPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            } while (dataPath.length() < 0);

            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Glide.with(Objects.requireNonNull(getContext()))
                .load(dataPath)
                .apply(options)
                .into(mImageViewOpenAlbum);

        mImageViewQuestion.setOnClickListener(this);
        mImageViewFlash.setOnClickListener(this);
        mImageViewOpenAlbum.setOnClickListener(this);

        mScannerView = new ZXingScannerView(getContext()) {
            @Override
            protected IViewFinder createViewFinderView(Context context) {
                return new CustomViewFinderView(context, null);
            }
        };
        contentFrame.addView(mScannerView);

// Здесь мы добавляем собственную рамку
        ImageView customFrame = new ImageView(getContext());
        customFrame.setImageResource(R.drawable.ic_frame);
        customFrame.setScaleX(0.5f);    // уменьшить размер рамки на 50% по оси X
        customFrame.setScaleY(0.5f);    // уменьшить размер рамки на 50% по оси Y
        contentFrame.addView(customFrame);



    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文