app/src/main/java/com/demo/selfqrcode/DecodeHandler.java
/**
   * 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.
   */
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.
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();
    Result rawResult = null;
    byte[] rotatedData = new byte[data.length];
  	byte[] rotatedData_front1 = new byte[data.length];
  	byte[] rotatedData_front2 = 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];
	    	rotatedData_front1[x * height + height - y - 1] = data[width - 1 - x + y * width];
	    	rotatedData_front2[x * height + height - y - 1] = data[width - 1 - x + (height - 1 - y) * width];
	    }
	}
	PlanarYUVLuminanceSource source_front = null;
	PlanarYUVLuminanceSource source = null;
	if(CameraManager.isFront()){
		source_front = activity.getCameraManager().buildLuminanceSource(rotatedData_front2, height, width);
		if(source_front != null){
			rawResult = getRawResult(source_front);
		}
		source_front = activity.getCameraManager().buildLuminanceSource(rotatedData_front1, height, width);
		if(rawResult == null){
			if(source_front != null){
				rawResult = getRawResult(source_front);
			}
		}
	}else{
		source = activity.getCameraManager().buildLuminanceSource(rotatedData, height, width);
		if(source != null){
			rawResult = getRawResult(source);
		}
	}
    Handler handler = activity.getHandler();
    if (rawResult != null) {
      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);
        Bundle bundle = new Bundle();
        Bitmap grayscaleBitmap = null;
        if(CameraManager.isFront()){
        	grayscaleBitmap = toBitmap(source_front, source_front.renderCroppedGreyscaleBitmap());
        }else{
        	grayscaleBitmap = toBitmap(source, source.renderCroppedGreyscaleBitmap());
        }
        bundle.putParcelable(DecodeThread.BARCODE_BITMAP, grayscaleBitmap);
        message.setData(bundle);
        message.sendToTarget();
      }
    } else {
      if (handler != null) {
        Message message = Message.obtain(handler, R.id.decode_failed);
        message.sendToTarget();
      }
    }
  }
private void decode ( byte [ ] data , int width , int height ) { long start = System . currentTimeMillis ( ) ; Result rawResult = null ; byte [ ] rotatedData = new byte [ data . length ] ; byte [ ] rotatedData_front1 = new byte [ data . length ] ; byte [ ] rotatedData_front2 = 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 ] ; rotatedData_front1 [ x * height + height - y - 1 ] = data [ width - 1 - x + y * width ] ; rotatedData_front2 [ x * height + height - y - 1 ] = data [ width - 1 - x + ( height - 1 - y ) * width ] ; } } PlanarYUVLuminanceSource source_front = null ; PlanarYUVLuminanceSource source = null ; if ( CameraManager . isFront ( ) ) { source_front = activity . getCameraManager ( ) . buildLuminanceSource ( rotatedData_front2 , height , width ) ; if ( source_front != null ) { rawResult = getRawResult ( source_front ) ; } source_front = activity . getCameraManager ( ) . buildLuminanceSource ( rotatedData_front1 , height , width ) ; if ( rawResult == null ) { if ( source_front != null ) { rawResult = getRawResult ( source_front ) ; } } } else { source = activity . getCameraManager ( ) . buildLuminanceSource ( rotatedData , height , width ) ; if ( source != null ) { rawResult = getRawResult ( source ) ; } } Handler handler = activity . getHandler ( ) ; if ( rawResult != null ) { 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 ) ; Bundle bundle = new Bundle ( ) ; Bitmap grayscaleBitmap = null ; if ( CameraManager . isFront ( ) ) { grayscaleBitmap = toBitmap ( source_front , source_front . renderCroppedGreyscaleBitmap ( ) ) ; } else { grayscaleBitmap = toBitmap ( source , source . renderCroppedGreyscaleBitmap ( ) ) ; } bundle . putParcelable ( DecodeThread . BARCODE_BITMAP , grayscaleBitmap ) ; message . setData ( bundle ) ; message . sendToTarget ( ) ; } } else { if ( handler != null ) { Message message = Message . obtain ( handler , R . id . decode_failed ) ; message . sendToTarget ( ) ; } } }
Decode the data within the viewfinder rectangle, and time how long it took.
Decode the data within the viewfinder rectangle and time how long it took .
// hack: rotate the image // Don't log the barcode contents for security.