Android二維碼掃描的簡單實現(xiàn)及源碼分析

二維碼掃描最近兩年簡直是風(fēng)靡移動互聯(lián)網(wǎng)時代厨喂,尤其在國內(nèi)發(fā)展神速和措。圍繞條碼掃碼功能,首先說說通過本文你可以知道啥杯聚。一臼婆,如何在你的APP中快速集成一維碼抒痒、二維碼掃描功能幌绍;二,大概了解條碼掃描功能的實現(xiàn)原理以及理解各個模塊的代碼。但是傀广,本文不包含條碼的編解碼原理颁独,如有興趣,請自行查閱伪冰。
條碼掃描功能的快速集成
默認你使用了Android Stuidio進行開發(fā)誓酒,直接使用這個開源項目SimpleZXing,它是在ZXing庫的作者為Android寫的條碼掃描APP Barcode Scanner的基礎(chǔ)上優(yōu)化而成的。你可以通過簡單的兩步就可以實現(xiàn)條碼掃描的功能贮聂。
1.添加項目依賴

1

compile 'com.acker:simplezxing:1.5'

2.在你想調(diào)起條碼掃描界面的地方(比如YourActivity)靠柑,調(diào)起二維碼掃描界面CaptureActivity

1

startActivityForResult(new Intent(YourActivity.this, CaptureActivity.class), CaptureActivity.REQ_CODE)

然后就會打開如下這個界面:

條碼掃描界面

將條碼置于框內(nèi),掃描成功后會將解碼得到的字符串返回給調(diào)起者吓懈,所以你只需要在你的Activity的onActivityResult()方法里拿到它進行后續(xù)操作即可歼冰。
當(dāng)然SimpleZXing目前還支持一些設(shè)置項,包括攝像頭是否開啟曝光耻警,掃碼成功后是否震動隔嫡,是否發(fā)聲,閃光燈模式自動甘穿、常開腮恩、常關(guān),屏幕自動旋轉(zhuǎn)温兼、橫屏秸滴、豎屏。
同時募判,雖然該項目已經(jīng)在manifest里申明了所需的照相機權(quán)限缸榛,但是在Android 6.0以上系統(tǒng)中你仍然需要自己處理動態(tài)權(quán)限管理。所以一個標(biāo)準(zhǔn)的使用方式如以下代碼所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

package com.acker.simplezxing.demo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.acker.simplezxing.activity.CaptureActivity;

public class MainActivity extends AppCompatActivity {

private static final int REQ_CODE_PERMISSION = 0x1111;
private TextView tvResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView) findViewById(R.id.tv_result);
Button btn = (Button) findViewById(R.id.btn_sm);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Open Scan Activity
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// Do not have the permission of camera, request it.
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQ_CODE_PERMISSION);
} else {
// Have gotten the permission
startCaptureActivityForResult();
}
}
});
}

private void startCaptureActivityForResult() {
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
Bundle bundle = new Bundle();
bundle.putBoolean(CaptureActivity.KEY_NEED_BEEP, CaptureActivity.VALUE_BEEP);
bundle.putBoolean(CaptureActivity.KEY_NEED_VIBRATION, CaptureActivity.VALUE_VIBRATION);
bundle.putBoolean(CaptureActivity.KEY_NEED_EXPOSURE, CaptureActivity.VALUE_NO_EXPOSURE);
bundle.putByte(CaptureActivity.KEY_FLASHLIGHT_MODE, CaptureActivity.VALUE_FLASHLIGHT_OFF);
bundle.putByte(CaptureActivity.KEY_ORIENTATION_MODE, CaptureActivity.VALUE_ORIENTATION_AUTO);
bundle.putBoolean(CaptureActivity.KEY_SCAN_AREA_FULL_SCREEN, CaptureActivity.VALUE_SCAN_AREA_FULL_SCREEN);
bundle.putBoolean(CaptureActivity.KEY_NEED_SCAN_HINT_TEXT, CaptureActivity.VALUE_SCAN_HINT_TEXT);
intent.putExtra(CaptureActivity.EXTRA_SETTING_BUNDLE, bundle);
startActivityForResult(intent, CaptureActivity.REQ_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQ_CODE_PERMISSION: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// User agree the permission
startCaptureActivityForResult();
} else {
// User disagree the permission
Toast.makeText(this, "You must agree the camera permission request before you use the code scan function", Toast.LENGTH_LONG).show();
}
}
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CaptureActivity.REQ_CODE:
switch (resultCode) {
case RESULT_OK:
tvResult.setText(data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT));
//or do sth
break;
case RESULT_CANCELED:
if (data != null) {
// for some reason camera is not working correctly
tvResult.setText(data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT));
}
break;
}
break;
}
}
}

以上說了如何通過使用SimpleZXing開源項目來快速實現(xiàn)條碼掃描功能兰伤,當(dāng)然開發(fā)者可能會因為一些特定的需求需要修改某些地方的代碼内颗,如UI等等,那么下面我會帶大家大致講解一下這個開源項目的代碼敦腔,使大家更進一步了解條碼掃描的實現(xiàn)機制均澳,同時方便大家在它基礎(chǔ)之上進行修改。
SimpleZXing關(guān)鍵代碼分析
其實條碼掃描的過程很容易理解符衔,就是將攝像頭捕捉到的預(yù)覽幀數(shù)組進行處理找前,發(fā)現(xiàn)其中的一維碼或二維碼并進行解碼。但是就是在攝像頭捕捉數(shù)據(jù)的過程中有幾個重要的地方需要大家注意判族。我們倒過來分析這個過程躺盛。
1.DecodeHandler.class中的decode()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

/**

  • Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
  • reuse the same reader objects from one decode to the next.
  • @param data The YUV preview frame.
  • @param width The width of the preview frame.
  • @param height The height of the preview frame.
    */
    private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    if (width < height) {
    // portrait
    byte[] rotatedData = new byte[data.length];
    for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++)
    rotatedData[y * width + width - x - 1] = data[y + x * height];
    }
    data = rotatedData;
    }
    Result rawResult = null;
    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
    if (source != null) {
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
    rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
    // continue
    } finally {
    multiFormatReader.reset();
    }
    }
    Handler handler = activity.getHandler();
    if (rawResult != null) {
    // Don't log the barcode contents for security.
    long end = System.currentTimeMillis();
    Log.d(TAG, "Found barcode in " + (end - start) + " ms");
    if (handler != null) {
    Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
    message.sendToTarget();
    }
    } else {
    if (handler != null) {
    Message message = Message.obtain(handler, R.id.decode_failed);
    message.sendToTarget();
    }
    }
    }

顯然,這個時候處理解碼的線程對應(yīng)的Handler已經(jīng)拿到了預(yù)覽幀的byte數(shù)組以及預(yù)覽幀的高和寬形帮。在這個方法中槽惫,我們首先根據(jù)此時屏幕是橫屏還是豎屏對預(yù)覽幀數(shù)組進行了一個預(yù)處理周叮。
因為在Android設(shè)備上,存在以下幾個概念
屏幕方向:在Android系統(tǒng)中界斜,屏幕的左上角是坐標(biāo)系統(tǒng)的原點(0,0)坐標(biāo)仿耽。原點向右延伸是X軸正方向,原點向下延伸是Y軸正方向各薇。
相機傳感器方向:手機相機的圖像數(shù)據(jù)都是來自于攝像頭硬件的圖像傳感器项贺,這個傳感器在被固定到手機上后有一個默認的取景方向,坐標(biāo)原點位于手機橫放時的左上角峭判,即與橫屏應(yīng)用的屏幕X方向一致开缎。換句話說,與豎屏應(yīng)用的屏幕X方向呈90度角林螃。
相機的預(yù)覽方向:由于手機屏幕可以360度旋轉(zhuǎn)啥箭,為了保證用戶無論怎么旋轉(zhuǎn)手機都能看到“正確”的預(yù)覽畫面(這個“正確”是指顯示在UI預(yù)覽界面的畫面與人眼看到的眼前的畫面是一致的),Android系統(tǒng)底層根據(jù)當(dāng)前手機屏幕的方向?qū)D像傳感器采集到的數(shù)據(jù)進行了旋轉(zhuǎn)處理治宣,然后才送給顯示系統(tǒng)急侥,因此可以保證預(yù)覽畫面始終“正確”。在相機API中可以通過setDisplayOrientation()設(shè)置相機預(yù)覽方向侮邀。在默認情況下坏怪,這個值為0,與圖像傳感器一致绊茧。因此對于橫屏應(yīng)用來說铝宵,由于屏幕方向和預(yù)覽方向一致,預(yù)覽圖像不會顛倒90度华畏。但是對于豎屏應(yīng)用鹏秋,屏幕方向和預(yù)覽方向垂直,所以會出現(xiàn)顛倒90度現(xiàn)象亡笑。為了得到正確的預(yù)覽畫面侣夷,必須通過API將相機的預(yù)覽方向旋轉(zhuǎn)90,保持與屏幕方向一致仑乌,如圖3所示百拓。

也就是說,相機得到的圖像數(shù)據(jù)始終是一個橫屏的姿態(tài)晰甚,當(dāng)手機處于豎屏?xí)r衙传,即使我們通過設(shè)置在屏幕上看到的拍攝畫面是準(zhǔn)確的,沒有90度翻轉(zhuǎn)的厕九,我們通過API得到的圖像數(shù)據(jù)仍然是基于橫屏的蓖捶,因此在判斷到width < height即屏幕處于豎屏狀態(tài)時,我們首先對byte數(shù)組進行一個手動90度旋轉(zhuǎn)扁远,然后將結(jié)果構(gòu)造為一個PlanarYUVLuminanceSource對象俊鱼,進行真正的解析處理去了刻像,這里我們就不管了。
然后再看這個預(yù)覽幀數(shù)據(jù)是怎么來的亭引。
2.PreviewCallback.class中的onPreviewFrame()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Point cameraResolution = configManager.getCameraResolution();
Handler thePreviewHandler = previewHandler;
if (cameraResolution != null && thePreviewHandler != null) {
Message message;
Point screenResolution = configManager.getScreenResolution();
if (screenResolution.x < screenResolution.y){
// portrait
message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.y,
cameraResolution.x, data);
} else {
// landscape
message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x,
cameraResolution.y, data);
}
message.sendToTarget();
previewHandler = null;
} else {
Log.d(TAG, "Got preview callback, but no handler or resolution available");
}
}

這個容易理解,就是系統(tǒng)Camera.PreviewCallback接口皮获,并實現(xiàn)了回調(diào)方法焙蚓,每次獲取到預(yù)覽幀時將圖像數(shù)據(jù)進行回調(diào),同樣區(qū)分了橫豎屏的情況以方便上文decode時的預(yù)處理洒宝。這里出現(xiàn)了cameraResolution和screenResolution兩個對象购公,我們接下來看看它們分別是什么。
3.CameraConfigurationManager.class中的initFromCameraParameters()方法
我們可以看到雁歌,上面提到的cameraResolution和screenResolution是在CameraConfigurationManager.class中的initFromCameraParameters()方法中得到的宏浩。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

/**

  • Reads, one time, values from the camera that are needed by the app.
    */
    void initFromCameraParameters(OpenCamera camera) {
    Camera.Parameters parameters = camera.getCamera().getParameters();
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    int displayRotation = display.getRotation();
    int cwRotationFromNaturalToDisplay;
    switch (displayRotation) {
    case Surface.ROTATION_0:
    cwRotationFromNaturalToDisplay = 0;
    break;
    case Surface.ROTATION_90:
    cwRotationFromNaturalToDisplay = 90;
    break;
    case Surface.ROTATION_180:
    cwRotationFromNaturalToDisplay = 180;
    break;
    case Surface.ROTATION_270:
    cwRotationFromNaturalToDisplay = 270;
    break;
    default:
    // Have seen this return incorrect values like -90
    if (displayRotation % 90 == 0) {
    cwRotationFromNaturalToDisplay = (360 + displayRotation) % 360;
    } else {
    throw new IllegalArgumentException("Bad rotation: " + displayRotation);
    }
    }
    Log.i(TAG, "Display at: " + cwRotationFromNaturalToDisplay);

int cwRotationFromNaturalToCamera = camera.getOrientation();
Log.i(TAG, "Camera at: " + cwRotationFromNaturalToCamera);

// Still not 100% sure about this. But acts like we need to flip this:
if (camera.getFacing() == CameraFacing.FRONT) {
cwRotationFromNaturalToCamera = (360 - cwRotationFromNaturalToCamera) % 360;
Log.i(TAG, "Front camera overriden to: " + cwRotationFromNaturalToCamera);
}

cwRotationFromDisplayToCamera =
(360 + cwRotationFromNaturalToCamera - cwRotationFromNaturalToDisplay) % 360;
Log.i(TAG, "Final display orientation: " + cwRotationFromDisplayToCamera);
int cwNeededRotation;
if (camera.getFacing() == CameraFacing.FRONT) {
Log.i(TAG, "Compensating rotation for front camera");
cwNeededRotation = (360 - cwRotationFromDisplayToCamera) % 360;
} else {
cwNeededRotation = cwRotationFromDisplayToCamera;
}
Log.i(TAG, "Clockwise rotation from display to camera: " + cwNeededRotation);

Point theScreenResolution = new Point();
display.getSize(theScreenResolution);
screenResolution = theScreenResolution;
Log.i(TAG, "Screen resolution in current orientation: " + screenResolution);
cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
Log.i(TAG, "Camera resolution: " + cameraResolution);
bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
Log.i(TAG, "Best available preview size: " + bestPreviewSize);
}

這個方法中前面的大段通過屏幕方向和攝像頭成像方向來計算預(yù)覽畫面的旋轉(zhuǎn)度數(shù),從而保證預(yù)覽得到的畫面隨著手機的旋轉(zhuǎn)或者前后置攝像頭的更換而保持正確的顯示靠瞎,當(dāng)然比庄,我們也可以看到screenResolution是通過Display類來獲取到的Point對象。它的x乏盐,y值就分別代表當(dāng)前屏幕的橫向和縱向的像素值佳窑,當(dāng)然這個是和屏幕方向有關(guān)系的。然后可以看到另外兩個Point對象cameraResolution以及bestPreviewSize是通過CameraConfigurationUtils.class中的findBestPreviewSizeValue()方法得到的父能,那我們再來看這個方法神凑。
4.CameraConfigurationUtils.class中的findBestPreviewSizeValue()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {

List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
if (rawSupportedSizes == null) {
Log.w(TAG, "Device returned no supported preview sizes; using default");
Camera.Size defaultSize = parameters.getPreviewSize();
if (defaultSize == null) {
throw new IllegalStateException("Parameters contained no preview size!");
}
return new Point(defaultSize.width, defaultSize.height);
}

// Sort by size, descending
List<Camera.Size> supportedPreviewSizes = new ArrayList<>(rawSupportedSizes);
Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size a, Camera.Size b) {
int aPixels = a.height * a.width;
int bPixels = b.height * b.width;
if (bPixels < aPixels) {
return -1;
}
if (bPixels > aPixels) {
return 1;
}
return 0;
}
});

if (Log.isLoggable(TAG, Log.INFO)) {
StringBuilder previewSizesString = new StringBuilder();
for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
previewSizesString.append(supportedPreviewSize.width).append('x')
.append(supportedPreviewSize.height).append(' ');
}
Log.i(TAG, "Supported preview sizes: " + previewSizesString);
}

double screenAspectRatio = screenResolution.x / (double) screenResolution.y;
// Remove sizes that are unsuitable
Iterator<Camera.Size> it = supportedPreviewSizes.iterator();
while (it.hasNext()) {
Camera.Size supportedPreviewSize = it.next();
int realWidth = supportedPreviewSize.width;
int realHeight = supportedPreviewSize.height;
if (realWidth * realHeight < MIN_PREVIEW_PIXELS) {
it.remove();
continue;
}
boolean isScreenPortrait = screenResolution.x < screenResolution.y;
int maybeFlippedWidth = isScreenPortrait ? realHeight : realWidth;
int maybeFlippedHeight = isScreenPortrait ? realWidth : realHeight;
double aspectRatio = (double) maybeFlippedWidth / (double) maybeFlippedHeight;
double distortion = Math.abs(aspectRatio - screenAspectRatio);
if (distortion > MAX_ASPECT_DISTORTION) {
it.remove();
continue;
}

if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
Point exactPoint = new Point(realWidth, realHeight);
Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
return exactPoint;
}
}

// If no exact match, use largest preview size. This was not a great idea on older devices because
// of the additional computation needed. We're likely to get here on newer Android 4+ devices, where
// the CPU is much more powerful.
if (!supportedPreviewSizes.isEmpty()) {
Camera.Size largestPreview = supportedPreviewSizes.get(0);
Point largestSize = new Point(largestPreview.width, largestPreview.height);
Log.i(TAG, "Using largest suitable preview size: " + largestSize);
return largestSize;
}

// If there is nothing at all suitable, return current preview size
Camera.Size defaultPreview = parameters.getPreviewSize();
if (defaultPreview == null) {
throw new IllegalStateException("Parameters contained no preview size!");
}
Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);
Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);
return defaultSize;
}

可以看出所謂bestPreviewSize就是將相機支持的預(yù)覽分辨率都獲取到然后找一個和屏幕分辨率最接近的最為最終的結(jié)果,當(dāng)然何吝,同樣溉委,要有橫豎屏的處理。那么上面步驟3獲取到的這些Point對象等等還有什么用呢爱榕,其實它們都將作為相機預(yù)覽及顯示的參數(shù)設(shè)置給Camera對象瓣喊,如以下這個方法:
5.CameraConfigurationManager.class中的setDesiredCameraParameters()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

void setDesiredCameraParameters(OpenCamera camera, boolean safeMode) {
Camera theCamera = camera.getCamera();
Camera.Parameters parameters = theCamera.getParameters();
if (parameters == null) {
Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
return;
}
Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

if (safeMode) {
Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
}
initializeTorch(parameters, safeMode);
CameraConfigurationUtils.setFocus(
parameters,
true,
true,
safeMode);
if (!safeMode) {
CameraConfigurationUtils.setBarcodeSceneMode(parameters);
CameraConfigurationUtils.setVideoStabilization(parameters);
CameraConfigurationUtils.setFocusArea(parameters);
CameraConfigurationUtils.setMetering(parameters);
}
parameters.setPreviewSize(bestPreviewSize.x, bestPreviewSize.y);
theCamera.setParameters(parameters);
theCamera.setDisplayOrientation(cwRotationFromDisplayToCamera);
Camera.Parameters afterParameters = theCamera.getParameters();
Camera.Size afterSize = afterParameters.getPreviewSize();
if (afterSize != null && (bestPreviewSize.x != afterSize.width || bestPreviewSize.y != afterSize.height)) {
Log.w(TAG, "Camera said it supported preview size " + bestPreviewSize.x + 'x' + bestPreviewSize.y +
", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height);
bestPreviewSize.x = afterSize.width;
bestPreviewSize.y = afterSize.height;
}
}

其實很明了了,這個方法是將獲取到的那些參數(shù)整合成Parameters對象set到Camera里黔酥。至此型宝,我們大概說了兩個問題,一個是如何獲取并給相機設(shè)置參數(shù)絮爷,另一個是如何獲取攝像頭的預(yù)覽數(shù)據(jù)并進行處理趴酣,接下來還有一個很重要的點需要說明,那就是我們雖然獲取了整個預(yù)覽幀的數(shù)據(jù)準(zhǔn)備對其解析坑夯,但實際上岖寞,對于條碼掃描來說,真正被處理的其實只是掃描框內(nèi)的那部分圖片或者說數(shù)據(jù)柜蜈,所以我們在掃描的時候也必須將條碼置于框框內(nèi)仗谆,那么這就涉及到了兩個部分指巡,一個是在屏幕上繪制這樣一個矩形框,另一個是在預(yù)覽幀里提取框內(nèi)的數(shù)據(jù)隶垮。這兩點分別由以下方法實現(xiàn)藻雪。
6.CameraManager.getFramingRect()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

/**

  • Calculates the framing rect which the UI should draw to show the user where to place the
  • barcode. This target helps with alignment as well as forces the user to hold the device
  • far enough away to ensure the image will be in focus.
  • @return The rectangle to draw on screen in window coordinates.
    */
    public synchronized Rect getFramingRect() {
    if (framingRect == null) {
    if (camera == null) {
    return null;
    }
    Point screenResolution = configManager.getScreenResolution();
    if (screenResolution == null) {
    // Called early, before init even finished
    return null;
    }

int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
int height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_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;
}

這個是UI中的方框Rect對象的構(gòu)造,很簡單了狸吞,就是根據(jù)屏幕分辨率然后按照一個固定的比例來設(shè)置方框大小勉耀。這個方法在方框的自定義View繪制時調(diào)用。
7.CameraManager.class的getFramingRectInPreview()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

/**

  • Like {@link #getFramingRect} but coordinates are in terms of the preview frame,
  • not UI / screen.
  • @return {@link Rect} expressing barcode scan area in terms of the preview size
    */
    public synchronized Rect getFramingRectInPreview() {
    if (framingRectInPreview == null) {
    Rect framingRect = getFramingRect();
    if (framingRect == null) {
    return null;
    }
    Rect rect = new Rect(framingRect);
    Point cameraResolution = configManager.getCameraResolution();
    Point screenResolution = configManager.getScreenResolution();
    if (cameraResolution == null || screenResolution == null) {
    // Called early, before init even finished
    return null;
    }
    if (screenResolution.x < screenResolution.y) {
    // portrait
    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;
    } else {
    // landscape
    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;
    }
    framingRectInPreview = rect;

}
return framingRectInPreview;
}

這個是預(yù)覽幀方框Rect對象的構(gòu)造蹋偏,其實也很簡單便斥,就是因為相機盧蘭幀分辨率和屏幕顯示分辨率可能不一致,因此首先計算這兩者的比例威始,然后再按比例對步驟6中的UI方框進行縮放枢纠,同樣,計算比例的時候要區(qū)分橫豎屏黎棠。這個方法是在buildLuminanceSource()中調(diào)用的晋渺,也就是步驟1中的構(gòu)造PlanarYUVLuminanceSource對象時,其實還傳入了這一Rect對象脓斩,來代表有效數(shù)據(jù)些举。
看完是不是有點點亂,因為本文沒有系統(tǒng)的講解俭厚,只是將所涉及內(nèi)容的一些關(guān)鍵點比如Android Camera的使用户魏,以及相應(yīng)的橫豎屏的區(qū)別處理做了介紹,真正核心的條碼解碼算法并沒有深入挪挤,“淺嘗輒止”了叼丑,就醬紫吧,有什么問題歡迎大家討論扛门。
另鸠信,轉(zhuǎn)載請注明出處!文中若有什么錯誤希望大家探討指正论寨!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末星立,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子葬凳,更是在濱河造成了極大的恐慌绰垂,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件火焰,死亡現(xiàn)場離奇詭異劲装,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門占业,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绒怨,“玉大人,你說我怎么就攤上這事谦疾∧硝澹” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵念恍,是天一觀的道長六剥。 經(jīng)常有香客問我,道長樊诺,這世上最難降的妖魔是什么仗考? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任音同,我火速辦了婚禮词爬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘权均。我一直安慰自己顿膨,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布叽赊。 她就那樣靜靜地躺著恋沃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪必指。 梳的紋絲不亂的頭發(fā)上囊咏,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機與錄音塔橡,去河邊找鬼梅割。 笑死,一個胖子當(dāng)著我的面吹牛葛家,可吹牛的內(nèi)容都是我干的户辞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼癞谒,長吁一口氣:“原來是場噩夢啊……” “哼底燎!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起弹砚,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤双仍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后桌吃,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體殊校,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年读存,在試婚紗的時候發(fā)現(xiàn)自己被綠了为流。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片呕屎。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖敬察,靈堂內(nèi)的尸體忽然破棺而出秀睛,到底是詐尸還是另有隱情,我是刑警寧澤莲祸,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布蹂安,位于F島的核電站,受9級特大地震影響锐帜,放射性物質(zhì)發(fā)生泄漏田盈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一缴阎、第九天 我趴在偏房一處隱蔽的房頂上張望允瞧。 院中可真熱鬧,春花似錦蛮拔、人聲如沸述暂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽畦韭。三九已至,卻和暖如春肛跌,著一層夾襖步出監(jiān)牢的瞬間艺配,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工衍慎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留转唉,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓西饵,卻偏偏與公主長得像酝掩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子眷柔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容