2017-10-11 6 views
0

Google Play 서비스를 사용하여 BarcodeReader를 개발하려고하며 Zxing 샘플과 같은 카메라 표면 배경을 얻고 싶습니다. zxing sample image배경색을보기의 제한된 영역으로 변경 - Android

실제로 검정 투명 배경 및 흰색 사각형 캔버스 볼 및이 Rect Rect 투명하게, 그래서 내 Rectangle 경계없이보기 배경을 변경할 수 있습니다.

enter image description here

내가 몇 줄

public class MyView extends View { 

private Paint paint; 
private Path path; 
private int width, height; 
Canvas canvas; 
private Rect rectangle; 


public MyView(Context context) { 
    super(context); 
    init(); 
} 

public MyView(Context context, @Nullable AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(); 
} 

private void init() { 
    paint = new Paint(); 
    paint.setColor(Color.RED); 
    paint.setStrokeWidth(10); 
    paint.setStyle(Paint.Style.STROKE); 
    this.setBackgroundColor(getResources().getColor(R.color.black60)); 

} 


@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    this.canvas = canvas; 
    drawMiddleLine(); 
    drawLeftUpCorner(); 
    drawLeftDownCorner(); 
    drawRightUpCorner(); 
    drawRightDownCorner(); 
    drawRectSample(); 
} 

private void drawRectSample() { 
    int x = width/6; 
    int y = height/4; 
    int widthLength = x*5; 
    int heightLenght = y* 3; 
    Paint paint = new Paint(); 
    // create a rectangle that we'll draw later 
    rectangle = new Rect(x, y, widthLength, heightLenght); 
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(getResources().getColor(R.color.white50)); 
    canvas.drawRect(rectangle, paint); 


} 

private void drawLeftUpCorner() { 
    if (width != 0 && height != 0) { 
     int startX = width/6; 
     int startY = height/4; 
     int endX = startX * 2; 
     int endY = startY; 
     canvas.drawLine(startX, startY, endX, endY, paint); 
     endX = startX; 
     endY += startX; 
     canvas.drawLine(startX, startY, endX, endY, paint); 
    } 
} 

private void drawLeftDownCorner() { 
    if (width != 0 && height != 0) { 
     int startX = width/6; 
     int startY = height/4 * 3; 
     int endX = startX * 2; 
     int endY = startY; 
     canvas.drawLine(startX, startY, endX, endY, paint); 
     endX = startX; 
     endY -= startX; 
     canvas.drawLine(startX, startY, endX, endY, paint); 

    } 
} 

private void drawRightUpCorner() { 
    if (width != 0 && height != 0) { 
     int startX = (width/6) * 4; 
     int startY = height/4; 
     int endX = startX + (width/6); 
     int endY = startY; 
     canvas.drawLine(startX, startY, endX, endY, paint); 
     startX = endX; 
     endY = startY + (width/6); 
     canvas.drawLine(startX, startY, endX, endY, paint); 
    } 
} 

private void drawRightDownCorner() { 
    if (width != 0 && height != 0) { 
     int startX = (width/6) * 4; 
     int startY = (height/4) * 3; 
     int endX = startX + (width/ 6); 
     int endY = startY; 
     canvas.drawLine(startX, startY, endX, endY, paint); 
     startX = endX; 
     endY -= width/6; 
     canvas.drawLine(startX, startY, endX, endY, paint); 

    } 
} 


private void drawMiddleLine() { 
    if (width != 0 && height != 0) { 
     int startX = width/3; 
     int startY = height/2; 
     int endX = startX * 2; 
     int endY = startY; 
     canvas.drawLine(startX, startY, endX, endY, paint); 
     paint.setColor(Color.GREEN); 
    } 
} 

public void setBounds(int width, int height) { 
    this.width = width; 
    this.height = height; 
} 

}는 CameraSourePreview 및 MYVIEW (사용자 정의보기 그냥 화면 라인을 그릴) 여기서

이 내 XML입니다 있습니다을 그릴 경우 내 사용자 정의보기입니다 :

<?xml version="1.0" encoding="utf-8"?>           
<RelativeLayout                
xmlns:android="http://schemas.android.com/apk/res/android"     
android:id="@+id/topLayout"            
android:orientation="vertical"            
android:layout_width="match_parent"          
android:layout_height="match_parent"          
android:keepScreenOn="true"            
>                   

<t_systems.qrlabs.camera.CameraSourcePreview        
    android:id="@+id/preview"            
    android:layout_width="match_parent"         
    android:layout_height="match_parent">         

</t_systems.qrlabs.camera.CameraSourcePreview>        


<t_systems.qrlabs.barcode.MyView           
    android:layout_width="match_parent"         
    android:id="@+id/myView"      
    android:layout_centerInParent="true"         
    android:layout_height="match_parent" />        
</RelativeLayout>                

어떤 도움이 필요합니까? 감사.

+0

당신이 지원하는 도서관이 https://github.com/zxing/zxing –

+0

이 요점은이 feature.Try을 구현해야 Zxyng를 사용하고 싶지 않습니다. Google Play 서비스 비전을 사용하고 있습니다 : 11.4.0, 감사합니다. –

+0

https://github.com/googlesamples/android-vision/tree/master/visionSamples/barcode를 참조하십시오. -reader/app/src/main –

답변

0

나는 역 채우기 경로를 해결

private void drawBlackBackgorund() { 
    int x = width/6; 
    int y = height/4; 
    int widthLength = x*5; 
    int heightLenght = y* 3; 

    RectF rect = new RectF(x,y,widthLength,heightLenght); 

    path.addRect(rect,Path.Direction.CW); 
    path.setFillType(Path.FillType.INVERSE_EVEN_ODD); 
    canvas.clipPath(path); 
    canvas.drawColor(getResources().getColor(R.color.black60)); 

} 

enter image description here