Sub qr_trace()
Set qr_bitmap = ActiveShape
boostStart "QR Redraw"
cnt = qr_get_count
If (cnt - 21) Mod 4 <> 0 Then alert = vbCr & "РАЗМЕР НЕ СООТВЕТСТВУЕТ СТАНДАРТУ!!!" & vbCr & vbCr
xxx = "Ваш qr-код похоже имеет размер: " & cnt & vbCr & _
"размеры QR-кодов по стандарту:" & vbCr & alert & _
"21 x 21, 25 x 25, 29 x 29, 33 x 33" & vbCr & _
"37 x 37, 41 x 41, 45 x 45, 49 x 49" & vbCr & _
"53 x 53, 57 x 57, 61 x 61, 65 x 65" & vbCr & _
"69 x 69, 73 x 73, 77 x 77, 81 x 81" & vbCr & _
"85 x 85, 89 x 89, 93 x 93, 97 x 97" & vbCr & _
"101 x 101, ..., 177 x 177" & vbCr & _
"введите размер одним числом:"
qr_sz = InputBox(xxx, , cnt)
If qr_sz = "" Then boostFinish: Exit Sub
If (qr_sz - 21) Mod 4 <> 0 Then MsgBox "Размер не соответствует стандарту", vbExclamation: boostFinish: Exit Sub
Dim sq As Shape, sr As New ShapeRange
With qr_bitmap
.SizeHeight = .SizeWidth
sq_sz = .SizeWidth / qr_sz
t = .TopY
l = .LeftX
End With
d = sq_sz / 3
For i = 1 To qr_sz
For j = 0 To qr_sz - 1
Set sq = ActiveVirtualLayer.CreateRectangle2(l + sq_sz * j, t - sq_sz * i, sq_sz, sq_sz)
sq.Outline.Type = cdrNoOutline
Set c = ActiveDocument.SampleColorInArea(sq.LeftX + d, sq.BottomY + d, sq.RightX - d, sq.TopY - d, 100, 100, cdrColorGray)
If c.Gray > 100 Then
sq.Delete
Else
sr.Add sq
End If
DoEvents
Next j
Next i
Set sr = ActiveDocument.LogCreateShapeRange(sr)
sr.Group
sr.ApplyUniformFill CreateCMYKColor(0, 0, 0, 100)
boostFinish
End Sub
Function qr_get_count() As Integer
Dim s As Shape, sr As New ShapeRange
p = ActiveShape.SizeHeight * 2 + ActiveShape.SizeWidth * 2
Set dup = ActiveShape.Duplicate
If dup.Bitmap.ResolutionX > 150 Then dup.Bitmap.Resample , , False, 150, 150 'загрубляем, т.к. на больших битмапах трейс может не запуститься
Set sr = dup.Bitmap.Trace(cdrTraceLineArt, , , cdrColorGray, , 2, , False).Finish
If sr.Shapes(1).Type = cdrGroupShape Then
Set sr = sr.UngroupAllEx()
End If
For i = sr.Count To 1 Step -1
If sr(i).Fill.UniformColor.Gray > 100 Or sr(i).Curve.Length > p Then sr(i).Delete: sr.Remove i
Next i
sr.Sort "@shape1.Top * 100 - @shape1.Left > @shape2.Top * 100 - @shape2.Left"
qr_get_count = Round(sr.SizeWidth / (sr(1).SizeWidth / 7))
dup.Delete
sr.Shapes.All.Delete
End Function
Private Sub boostStart(Optional ByVal unDo As String = "")
If unDo <> "" Then ActiveDocument.BeginCommandGroup unDo
Optimization = True
EventsEnabled = False
ActiveDocument.SaveSettings
ActiveDocument.PreserveSelection = False
End Sub
Private Sub boostFinish()
ActiveDocument.EndCommandGroup
ActiveDocument.PreserveSelection = True
ActiveDocument.RestoreSettings
EventsEnabled = True
Optimization = False
ActiveWindow.Refresh
Refresh
End Sub