3.CameraManager类的getFramingRectInPreview()方法中将以下代码替换:
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;
替换为
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;
4.DecodeHandler类的decode方法中在activity.getCameraManager().buildLuminanceSource()之前添加以下代码:
byte[] rotatedData = new byte[data.length];
for(int y = 0;y < height; y++) {
for(int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width;
width = height;
height = tmp;
data = rotatedData;
5.关键的一步,解决竖屏后图像拉伸问题。CameraConfigurationManager类的initFromCameraParameters()方法中:
在Log.i(TAG, "Screen resolution: " + screenResolution);之后添加以下代码:
Point screenResolutionForCamera = new Point();
screenResolutionForCamera.x = screenResolution.x;
screenResolutionForCamera.y = screenResolution.y;
if(screenResolution.x < screenResolution.y) {
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x;
}
同时修改下一句为cameraResolution = findBestPreviewSizeValue(parameters,screenResolutionForCamera);
此外manifest中别忘了设置android:screenOrientation=“portrait”,至此竖屏修改完毕。
第四步:扫描框位置和大小修改
此时的扫描框是竖直拉伸的矩形,很难看,我们可以将其修改为正方形或扁平型的。
int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
int height = findDesiredDimensionInRange(screenResolution.y,MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
替换为
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = (int)(metrics.widthPixels * 0.6);
int height = (int) (width * 0.9);
此处我们根据屏幕分辨率来定扫描框大小更灵活一点,同时将偏移量topOffset修改为(screenResolution.y - height)/4
第五步:扫描框四个角和扫描线条修改
示例代码中的线条是居中且不动的,我们可以将其修改为上下移动的扫描线,且可以改变线条的样式。
在自定义扫描布局ViewfinderView类中的onDraw()方法中绘制四个角,关键代码如下:
//画出四个角
paint.setColor(getResources().getColor(R.color.green));
//左上角
canvas.drawRect(frame.left,frame.top, frame.left + 15,frame.top+ 5,paint);
canvas.drawRect(frame.left,frame.top, frame.left + 5,frame.top + 15,paint);
//右上角
canvas.drawRect(frame.right- 15,frame.top, frame.right,frame.top + 5,paint);
canvas.drawRect(frame.right- 5,frame.top, frame.right,frame.top + 15,paint);
//左下角
canvas.drawRect(frame.left,frame.bottom - 5,frame.left + 15,frame.bottom,paint);
canvas.drawRect(frame.left,frame.bottom - 15,frame.left + 5,frame.bottom,paint);
//右下角
canvas.drawRect(frame.right- 15,frame.bottom - 5,frame.right,frame.bottom, paint);
canvas.drawRect(frame.right- 5,frame.bottom - 15,frame.right,frame.bottom, paint);
此外将扫描线条修改为上下扫描的线,关键代码如下:
if((i += 5)< frame.bottom - frame.top) {
/* 以下为用渐变线条作为扫描线 */
//渐变图为矩形
//mDrawable.setShape(GradientDrawable.RECTANGLE);
//渐变图为线型
//mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
//线型矩形的四个圆角半径
mDrawable.setCornerRadii(new float[] { 8, 8, 8, 8, 8, 8, 8, 8 });