android 二維碼掃描(zxing使用小結)
阿新 • • 發佈:2019-02-05
主要有三點:
1、介面繪製,包括4個邊角、上下滾動的橫線、在畫布上新增文字
2、調整二維碼掃描框的位置。
3、解決圖片被壓變形的問題。
1、介面繪製
ViewfinderView.java中onDraw()方法
@Override
public
void
onDraw(Canvas canvas) {
// 中間的掃描框,你要修改掃描框的大小,去CameraManager裡面修改
Rect frame = CameraManager.get().getFramingRect();
int
top = frame.top;
int
left = frame.left;
int
right = frame.right;
int
bottom = frame.bottom;
frame =
new
Rect(left , top, right , bottom );
// 初始化中間線滑動的最上邊和最下邊
if
(!isFirst) {
isFirst =
true
;
slideTop = frame.top;
slideBottom = frame.bottom;
}
// 獲取螢幕的寬和高
int
width = canvas.getWidth();
int
height = canvas.getHeight();
// 畫出掃描框外面的陰影部分,共四個部分,掃描框的上面到螢幕上面,掃描框的下面到螢幕下面
// 掃描框的左邊面到螢幕左邊,掃描框的右邊到螢幕右邊
// 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);
if
(resultBitmap !=
null
) {
// Draw the opaque result bitmap over the scanning rectangle
paint.setAlpha(OPAQUE);
canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);
}
else
{
// 畫掃描框邊上的角,總共8個部分
paint.setColor(Color.GREEN);
canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate,
frame.top + CORNER_WIDTH, paint);
canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top
+ ScreenRate, paint);
canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right,
frame.top + CORNER_WIDTH, paint);
canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top
+ ScreenRate, paint);
canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left
+ ScreenRate, frame.bottom, paint);
canvas.drawRect(frame.left, frame.bottom - ScreenRate,
frame.left + CORNER_WIDTH, frame.bottom, paint);
canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH,
frame.right, frame.bottom, paint);
canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate,
frame.right, frame.bottom, paint);
//繪製中間的線,每次重新整理介面,中間的線往下移動SPEEN_DISTANCE
slideTop += SPEEN_DISTANCE;
if
(slideTop >= frame.bottom){
slideTop = frame.top;
}
Rect lineRect =
new
Rect();
lineRect.left = frame.left;
lineRect.right = frame.right;
lineRect.top = slideTop;
lineRect.bottom = slideTop +
18
;
canvas.drawBitmap(((BitmapDrawable)(getResources().getDrawable(R.drawable.qrcode_scan_line))).getBitmap(),
null
, lineRect, paint);
//畫掃描框下面的字
// paint.setColor(Color.WHITE);
// paint.setTextSize(TEXT_SIZE * density);
// paint.setAlpha(0x40);
// paint.setTypeface(Typeface.create(System, Typeface.BOLD));
// String text = 將二維碼放入框內, 即可自動掃描;
// float textWidth = paint.measureText(text);
//
// canvas.drawText(text, (width - textWidth)/2, (float) (frame.bottom + (float)TEXT_PADDING_TOP *density), paint);
Collection<resultpoint> currentPossible = possibleResultPoints;
Collection<resultpoint> currentLast = lastPossibleResultPoints;
if
(currentPossible.isEmpty()) {
lastPossibleResultPoints =
null
;
}
else
{
possibleResultPoints =
new
HashSet<resultpoint>(
5
);
lastPossibleResultPoints = currentPossible;
paint.setAlpha(OPAQUE);
paint.setColor(resultPointColor);
for
(ResultPoint point : currentPossible) {
canvas.drawCircle(frame.left + point.getX(), frame.top
+ point.getY(),
6
.0f, paint);
}
}
if
(currentLast !=
null
) {
paint.setAlpha(OPAQUE /
2
);
paint.setColor(resultPointColor);
for
(ResultPoint point : currentLast) {
canvas.drawCircle(frame.left + point.getX(), frame.top
+ point.getY(),
3
.0f, paint);
}
}
//只重新整理掃描框的內容,其他地方不重新整理
postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top,
frame.right, frame.bottom);
}
}</resultpoint></resultpoint></resultpoint>
2、調整位置
如果不設定全屏,掃描框的位置和取圖的位置高度目測會有50px誤差, 不全屏公式:((Y - 標題欄 - 狀態列)/2 - 掃描框高度)/2 = y軸座標 只要不全屏:就會有誤差,導致框中的二維碼靠上或者遠離二維碼才會掃描成功 如果設定為全屏,就不會有誤差,請了解內情的大神告知筆者,想知道什麼原因造成 CameraManager.java 中 getFramingRect()方法public
Rect getFramingRect() {
Point screenResolution = configManager.getScreenResolution();
if
(framingRect ==
null
) {
if
(camera ==
null
) {
return
null
;
}
/* 確定在固定比例解析度下二維碼掃描框的寬高*/
int
width = screenResolution.x *
3
/
4
;
if
(width < MIN_FRAME_WIDTH) {
width = MIN_FRAME_WIDTH;
}
else
if
(width > MAX_FRAME_WIDTH) {
width = MAX_FRAME_WIDTH;
}
int
height = screenResolution.y *
3
/
4
;
if
(height < MIN_FRAME_HEIGHT) {
height = MIN_FRAME_HEIGHT;
}
else
if
(height > MAX_FRAME_HEIGHT) {
height = MAX_FRAME_HEIGHT;
}
//計算x軸y軸座標,可在此調整二維碼框的位置
int
leftOffset = (screenResolution.x - width) /
2
;
int
topOffset = ((screenResolution.y) /
2
- height) /
2
;
framingRect =
new
Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
Log.d(TAG, Calculated framing rect: + framingRect);
}
return
framingRect;
}
3、框中圖片變形
CameraConfigurationManager.java中void setDesiredCameraParameters(Camera camera)方法/**
* Sets the camera up to take preview images which are used for both preview
* and decoding. We detect the preview format here so that
* buildLuminanceSource() can build an appropriate LuminanceSource subclass.
* In the future we may want to force YUV420SP as it's the smallest, and the
* planar Y can be used for barcode scanning without a copy in some cases.
*/
void
setDesiredCameraParameters(Camera camera) {
// Camera.Parameters parameters = camera.getParameters();
// Log.d(TAG, Setting preview size: + cameraResolution);
// parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
// setFlash(parameters);
// setZoom(parameters);
// //setSharpness(parameters);
// //modify here
// camera.setDisplayOrientation(90);
// camera.setParameters(parameters);
Camera.Parameters parameters = camera.getParameters();
List<size> supportedPreviewSizes = parameters
.getSupportedPreviewSizes();
int
position =
0
;
if
(supportedPreviewSizes.size() >
2
) {
position = supportedPreviewSizes.size() /
2
+
1
;
// supportedPreviewSizes.get();
}
else
{
position = supportedPreviewSizes.size() /
2
;
}
int
width = supportedPreviewSizes.get(position).width;
int
height = supportedPreviewSizes.get(position).height;
Log.d(TAG, Setting preview size: + cameraResolution);
camera.setDisplayOrientation(
90
);
cameraResolution.x = width;
cameraResolution.y = height;
parameters.setPreviewSize(width, height);
setFlash(parameters);
setZoom(parameters);
// setSharpness(parameters);
camera.setParameters(parameters);
}</size>