Android 二維碼框4個角&上下滾動條&閃光燈

二維碼框4個角&上下滾動條&閃光燈

本文章是在一個大牛的demo上修改的,感謝大牛分享,zxing關鍵代碼都是一樣,只在標題上說的三個功能講解

 

成員屬性如下,方便下面講解:

/**
	 * 四個綠色邊角對應的長度
	 */
	private int ScreenRate;

	/**
	 * 四個綠色邊角對應的寬度
	 */
	private static final int CORNER_WIDTH = 5;
	/**
	 * 掃描框中的中間線的寬度
	 */
	private static final int MIDDLE_LINE_WIDTH = 6;

	/**
	 * 掃描框中的中間線的與掃描框左右的間隙
	 */
	private static final int MIDDLE_LINE_PADDING = 5;

	/**
	 * 中間那條線每次刷新移動的距離
	 */
	private static final int SPEEN_DISTANCE = 5;

	/**
	 * 手機的屏幕密度
	 */
	private static float density;
	/**
	 * 字體大小
	 */
	private static final int TEXT_SIZE = 16;
	/**
	 * 字體距離掃描框下面的距離
	 */
	private static final int TEXT_PADDING_TOP = 30;
	boolean isFirst;
	/**
	 * 中間滑動線的最頂端位置
	 */
	private int slideTop;

	/**
	 * 中間滑動線的最底端位置
	 */
	private int slideBottom;

 

ViewfinderView方法中進行初始化操作,涉及的代碼如下:

		density = context.getResources().getDisplayMetrics().density;
		// 將像素轉換成dp
		ScreenRate = (int) (15 * density)

(一) 二維碼框4個角&上下滾動條

         邊框顧名思義也就是onDraw()方法畫邊,使用畫筆的缺點是隻能爲兩點畫線,在四個角 ,畫4個獨立的直角

4個直角各兩條線,所以得畫8條線

         上下滾動條,用畫筆加載一張橫線的圖片就可以 這個不難理解

在onDraw()代碼如下:

	@Override
	public void onDraw(Canvas canvas) {
		// 中間的掃描框,你要修改掃描框的大小,去CameraManager裏面修改
		Rect frame = CameraManager.get().getFramingRect();
		if (frame == null) {
			return;
		}
		// 初始化中間線滑動的最上邊和最下邊
		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);
			
		}
	}

(二)閃光燈

         首先在carmra佈局中加一個按鈕或者圖片按鈕

       

<Button
            android:id="@+id/btn_light"
            android:layout_width="230dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_centerInParent="true"
            android:layout_marginBottom="25dp"
            android:text="打開閃光燈"
            android:textSize="15sp"
            android:textStyle="bold" />


注意的一點是系統中只能存在一個Camera對象 ,所以在CameraManager中加一個獲取Camera對象的方法

  

  public static Camera getCamera(){
	  return camera;
  }

聲明一個Camera屬性

初始化:

CameraManager.get().openDriver(surfaceHolder);
			camera = CameraManager.getCamera();

 

在操作掃描二維碼的activity中設置按鈕的監聽事件

		//開閉閃光燈事件  
		lightButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (lightButton.getText().toString().equals("打開閃光燈")) {
					parameter = camera.getParameters();
					parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
					camera.setParameters(parameter);
					lightButton.setText("關閉閃光燈");
				} else if (lightButton.getText().toString().equals("關閉閃光燈")) {
					parameter = camera.getParameters();
					parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
					camera.setParameters(parameter);
					lightButton.setText("打開閃光燈");
				}
			}
		});


 

 



<Button
            android:id="@+id/btn_light"
            android:layout_width="230dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_centerInParent="true"
            android:layout_marginBottom="25dp"
            android:text="打開閃光燈"
            android:textSize="15sp"
            android:textStyle="bold" />

 

發佈了62 篇原創文章 · 獲贊 5 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章