       }
       return contents;
     }
+
+    public function calculateEANChecksumDigit(number:Number):Number {
+      var checksum:Number = 0;
+      var weightedSumOfDigits:Number = 0;
+      var weight:Number = 3;
+      while (number > 0) {
+        var lastDigit:int = number % 10;
+        weightedSumOfDigits = weightedSumOfDigits + lastDigit * weight;
+        number = (number - lastDigit) / 10;
+        weight = weight == 3 ? 1 : 3;
+      }
+      var additionToTen:Number = weightedSumOfDigits % 10;
+      if (additionToTen > 0) {
+        checksum = 10 - additionToTen;
+      }
+      return checksum;
+    }
   }
 }

    }

    [Test(description="Encode a number, decode it, and compare.")]
    public function encodedEAN8shouldBeDecodedSuccessfully():void {
      var testCode:Number = 48512343;
      var decodedNumber:Number = encodeAndDecode(testCode);
      assertEquals(testCode, decodedNumber);
    }

    [Test(description="Bug: Endoding and decoding 1 fails")]
    public function shouldEncodeAndDecodeAnyValidEAN8number():void {
      var testCode:Number = 12345678;
      var decodedNumber:Number = encodeAndDecode(testCode);
      assertEquals(testCode, decodedNumber);
    }

    private function encodeAndDecode(testCode:Number):Number {


    private function generateImage(resultBits:Array, imageWidth:Number,
        imageHeight:Number):Bitmap {
      var bitmapData:BitmapData = new BitmapData(imageWidth, imageHeight, false,
          0xFFFFFF);
      var bitmap:Bitmap = new Bitmap(bitmapData);
