Barcode 掃瞄

      在〈Barcode 掃瞄〉中尚無留言

Google Mobile Vision

早期最常用的SDK有Zxing, 後來google開發了Google Mobile Vision將之取代 , 而且掃瞄速度更快.

下載SDK

https://github.com/avaneeshkumarmaurya/Barcode-Reader 按下clone or download, 解開後, 再由Android studio的File/New/import module, 選取解壓縮後的barcode-reader目錄

build.grandle

在build.grandle 加入如下設定

dependencies {
 // google vision gradle
 implementation 'com.google.android.gms:play-services-vision:15.0.2'
 implementation project(path: ':barcode-reader')
}

Google範例

以下代碼是Google套件的範例, 只需在 MainActivity加入如下代碼即可測試

public class MainActivity extends AppCompatActivity {
    final int BARCODE_READER_ACTIVITY_REQUEST=1000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent launchIntent = BarcodeReaderActivity.getLaunchIntent(this, true, false);
        startActivityForResult(launchIntent, BARCODE_READER_ACTIVITY_REQUEST);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != Activity.RESULT_OK) {
            Toast.makeText(this, "error in  scanning", Toast.LENGTH_SHORT).show();
            return;
        }

        if (requestCode == BARCODE_READER_ACTIVITY_REQUEST && data != null) {
            Barcode barcode = data.getParcelableExtra(BarcodeReaderActivity.KEY_CAPTURED_BARCODE);
            Toast.makeText(this, barcode.rawValue, Toast.LENGTH_SHORT).show();
        }

    }
}

實際專案

此部份為小編實際專案, 圖示如下

修正import

下圖引入的library中, 會發生編譯錯誤, 原因在於此library使用舊版的sdk編譯, 所以把相關的import刪除, 再於錯誤處按下Alt+Enter, 重新import新版sdk即可

barcode1

BarcodeReaderFragment修改

public void start(){
        CameraSource localCameraSource = this.mCameraSource;
        if (localCameraSource != null) {
            try
            {
                this.mPreview.start(localCameraSource, this.mGraphicOverlay);
            }
            catch (IOException localIOException)
            {
                Log.e(TAG, "Unable to start camera source.", localIOException);
                this.mCameraSource.release();
                this.mCameraSource = null;
            }
        }
    }
    public void stop(){
        CameraSourcePreview localCameraSourcePreview = this.mPreview;
        if (localCameraSourcePreview != null) {
            localCameraSourcePreview.stop();
        }
    }

ScanOverlay修改

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // draw transparent rect
        int cornerRadius = 0;
        Paint eraser = new Paint();
        eraser.setAntiAlias(true);
        eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));


        RectF rect = new RectF(left, top, dpToPx(rectWidth) + left, dpToPx(rectHeight) + top);
        canvas.drawRoundRect(rect, (float) cornerRadius, (float) cornerRadius, eraser);

        // draw horizontal line
        Paint line = new Paint();
        if(runFlag) {
            line.setColor(lineColor);
            line.setStrokeWidth(Float.valueOf(lineWidth));

            // draw the line to product animation
            if (endY >= top + dpToPx(rectHeight) + frames) {
                revAnimation = true;
            } else if (endY == top + frames) {
                revAnimation = false;
            }

            // check if the line has reached to bottom
            if (revAnimation) {
                endY -= frames;
            } else {
                endY += frames;
            }
            canvas.drawLine(left, endY, left + dpToPx(rectWidth), endY, line);
        }
        invalidate();
    }
    public void start()
    {
        this.runFlag = true;
    }

    public void stop()
    {
        this.runFlag = false;
    }

參考

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *