curtis
22小時前 10220027159dc90f19f7c62a7b84bb00c6453d4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
function TCB_IMGPSScanX.Get_Color: OLE_COLOR;
begin
  Result := OLE_COLOR(Color);
end;
 
procedure TCB_IMGPSScanX.Set_Color(Value: OLE_COLOR);
begin
  Color := TColor(Value);
end;
 
procedure TCB_IMGPSScanX.StatrTwainScan;
var ScanInfo    : TScanInfo;
   i : Integer;
begin
  if not Scanner.IsConfigured then
  begin
    ShowMessage(_Msg('TWAIN 掃瞄驅動尚未安裝'));
    Exit;
  end;
  FillChar(ScanInfo, SizeOf(ScanInfo), 0);
  ScanInfo.MultiPage := True;
  ScanInfo.ImageCount := 0;
  ScanInfo.Graphic := TTiffGraphic.Create;
  try
    ISB := nil; //規零
//ShowMessage(IntToStr(ScanDpi));
    Scanner.RequestedXDpi := ScanDpi;
    Scanner.RequestedYDpi := ScanDpi;
    Scanner.RequestedImageFormat := ScanColor;
    Scanner.ShowUI := TwainShowUI;
 
    Try
      Scanner.OpenSource;
      Scanner.Duplex := ScanDuplex; //雙面
 
      if FMode='SAMPLESCAN' then
        Scanner.Duplex:=False;
 
      //Scanner.FEEDERENABLED  := not ScanFlatCB.Checked;     // 先拿掉平台
      If ScanImgSetUse Then
      begin
        Scanner.ScanBrightness := ScanBright;
        Scanner.ScanContrast := ScanContrast;
      end;
    except
      Showmessage(_Msg('掃瞄器發生錯誤!!'));
      Scanner.CloseSource;
      Exit;
    end;
    try
      Try
        Scanner.AcquireWithSourceOpen( OnAcquire, LongInt(@ScanInfo));
      Except
        Scanner.CloseSource;
      end;
    finally
      Scanner.CloseSource;
    end;
  finally
  Scanner.CloseSource;
  ScanInfo.Graphic.Free;
  end;
end;
 
procedure TCB_IMGPSScanX.OnAcquire( const DibHandle    : THandle;
                               const XDpi         : Word;
                               const YDpi         : Word;
                               const CallBackData : LongInt );
var
  pScanInfo  : TpScanInfo;
  SaveFileName : String;
  SaveStream     : TFileStream;
  strResults : TBarcodeStringResult;
  DeleteStm  : TMemoryStream;
  isDelete   : Boolean;
  iGraphic,iGraphic_First,iGraphic_sec : TTiffGraphic;
  iRect : TRect;
  JpgGr : TJpegGraphic;
  i : Integer;
  TagTxt : String;
  function Deletepage(Graphic:TDibGraphic;BlankSize:Integer):Boolean;
  begin
    DeleteStm  := TMemoryStream.Create;
    DeleteStm.Seek(0,soFromBeginning);
    Graphic.AppendToStream(DeleteStm);
 
    //DeleteStm.LoadFromFile(Path+'temp.tif');
    //Isb1.Graphic.SaveToStream(DeleteStm);
    //Isb1.Graphic.AppendToStream(DeleteStm);
    if DeleteStm.Size < BlankSize Then
      Result:= True
    Else
      Result := False;
 
    DeleteStm.Free;
    //DeleteFile(Path+'temp.tif');
  end;
begin
  pScanInfo := TpScanInfo(CallBackData);
  isDelete    := False;
 
  if pScanInfo^.MultiPage then
  begin
    pScanInfo^.Graphic.AssignFromDibHandle(DibHandle);
    pScanInfo^.Graphic.XDotsPerInch := XDpi;
    pScanInfo^.Graphic.YDotsPerInch := YDpi;
    TagTxt := 'height:'+inttostr(pScanInfo^.Graphic.Height)+',width:'+inttostr(pScanInfo^.Graphic.Width);
    if pScanInfo^.Graphic.ImageFormat = ifBlackWhite then
    begin
      ImageScrollBox1.Graphic.Assign(pScanInfo^.Graphic);
      ImageScrollBox1NewGraphic(ImageScrollBox1.Graphic);
 
      pScanInfo^.Graphic.Compression := tcGroup4;
      MpsGetBarcode(pScanInfo^.Graphic,MpsBarcodeinf);
      For i := 1 to MpsBarcodeinf.count do
      begin
        If (MpsBarcodeinf.r180[i] <> 0) and (Length(MpsBarcodeinf.Text[i])=FormIDLength) Then
        begin
          Rotate(pScanInfo^.Graphic,MpsBarcodeinf.r180[i]);
          MpsGetBarcode(pScanInfo^.Graphic,MpsBarcodeinf);  //旋轉後再重取一次條碼資訊
          Break;
        end;
      end;
 
      //影像反向
      IF ScannerReverse then
        NegativeImg(pScanInfo^.Graphic);
      //傾斜矯正
      IF ScanDeskew Then
        DeskewImg(pScanInfo^.Graphic);
      //清黑邊
      IF BoardClear then
        CleanupBorder(pScanInfo^.Graphic);
    end
    else if pScanInfo^.Graphic.ImageFormat = ifTrueColor then
    begin
        //Ext := '.jpg';
        ImageScrollBox1.Graphic.Assign(pScanInfo^.Graphic);
        //ImageScrollBox1NewGraphic(ImageScrollBox1.Graphic);
        MpsGetBarcode(ISB_BW.Graphic,MpsBarcodeinf);
        For i := 1 to MpsBarcodeinf.count do
        begin
          If (MpsBarcodeinf.r180[i] <> 0) and (Length(MpsBarcodeinf.Text[i])=FormIDLength) Then
          begin
            Rotate(ISB_BW.Graphic,MpsBarcodeinf.r180[i]);
            MpsGetBarcode(ISB_BW.Graphic,MpsBarcodeinf);  //旋轉後再重取一次條碼資訊
            Break;
          end;
        end;
        pScanInfo^.Graphic.Compression := tcJpeg;
        pScanInfo^.Graphic.JpegQuality := FJpgCompression;
    end
    else if pScanInfo^.Graphic.ImageFormat = ifColor256 Then
    begin
      //Ext := '.jpg';
      ConvertToGray(pScanInfo^.Graphic);
      pScanInfo^.Graphic.Compression := tcJpeg;
      pScanInfo^.Graphic.JpegQuality := FJpgCompression;
    end
    else if pScanInfo^.Graphic.ImageFormat = ifGray256 Then
    begin
      //Ext := '.jpg';
      ImageScrollBox1.Graphic.Assign(pScanInfo^.Graphic);
      ImageScrollBox1NewGraphic(ImageScrollBox1.Graphic);
      MpsGetBarcode(ISB_BW.Graphic,MpsBarcodeinf);
      For i := 1 to MpsBarcodeinf.count do
      begin
        If (MpsBarcodeinf.r180[i] <> 0) and (Length(MpsBarcodeinf.Text[i])=FormIDLength) Then
        begin
          Rotate(ISB_BW.Graphic,MpsBarcodeinf.r180[i]);
          MpsGetBarcode(ISB_BW.Graphic,MpsBarcodeinf);  //旋轉後再重取一次條碼資訊
          Break;
        end;
      end;
      pScanInfo^.Graphic.Compression := tcJpeg;
      pScanInfo^.Graphic.JpegQuality := FJpgCompression;
//ShowMessage(IntToStr(pScanInfo^.Graphic.JpegQuality));
//if pScanInfo^.Graphic.Compression = tcJpeg then
//begin
//ShowMessage('jpg');
//end;
 
    end
    else
    begin
      //Ext := '.tif';
      pScanInfo^.Graphic.Compression := tcPackBits;
    end;
 
  end;
  //Application.ProcessMessages;
  iGraphic_First := TTiffGraphic.Create;
  iGraphic_sec := TTiffGraphic.Create;
  //iGraphic := TTiffGraphic.Create;
  try
    iGraphic_First.Assign(pScanInfo^.Graphic);
    //Application.ProcessMessages;
    if CheckNeedCrop(iGraphic_First) Then
    begin
      iRect.Left := pScanInfo^.Graphic.Width div 2;    //先取左邊的影像
      iRect.Top := 0;
      iRect.Right := pScanInfo^.Graphic.Width;
      iRect.Bottom := pScanInfo^.Graphic.Height;
      CropImg(iGraphic_First,iRect);
 
      iGraphic_Sec.Assign(pScanInfo^.Graphic);         //再取右邊的影像
      iRect.Left := 0;
      iRect.Top := 0;
      iRect.Right := pScanInfo^.Graphic.Width div 2;
      iRect.Bottom := pScanInfo^.Graphic.Height;
      CropImg(iGraphic_Sec,iRect);
    end;
    //iGraphic.Assign(iGraphic_First);
    iGraphic := iGraphic_First;
 
    if iGraphic.ImageFormat=ifGray256 then  //20180104
    begin
      iGraphic.Compression:=tcJPEG;
      iGraphic.JpegQuality:=FJpgCompression;
    end;
    if iGraphic.ImageFormat=ifTrueColor then  //20180104
    begin
      iGraphic.Compression:=tcJPEG;
      iGraphic.JpegQuality:=FJpgCompression;
    end;
 
//ShowMessage('WTF');
    while not iGraphic.IsEmpty do
    begin
      //Application.ProcessMessages;
      IF (not DeviceDelete) or (not Deletepage(iGraphic,DeviceDeleteSize)) Then
      begin
        ImageScrollBox1.Graphic.Assign(iGraphic);
        ImageScrollBox1NewGraphic(ImageScrollBox1.Graphic);
 
        MpsGetBarcode(ISB_BW.Graphic,MpsBarcodeinf);
        For i := 1 To MpsBarcodeinf.Count Do
        Begin
          If MpsBarcodeinf.r180[i] <> 0 Then // 依條碼角度轉影像
          Begin
            Rotate(iGraphic, MpsBarcodeinf.r180[i]);
            Break;
          End;
        End;
 
        if iGraphic.ImageFormat=ifGray256 then  //20180104 因此旋轉後變為回packbits 所以要改為jpeg
        begin
          iGraphic.Compression:=tcJPEG;
          iGraphic.JpegQuality:=FJpgCompression;
        end;
        if iGraphic.ImageFormat=ifTrueColor then
        begin
          iGraphic.Compression:=tcJPEG;
          iGraphic.JpegQuality:=FJpgCompression;
        end;
        PageEnd;
        IF PEFileName <> '' Then
        begin
          IF LowerCase(ExtractFileExt(PEFileName)) = '.tif' Then
          begin
            if FileExists( PEFileName ) then
              SaveStream := TFileStream.Create( PEFileName ,fmOpenReadWrite)
            Else
              SaveStream := TFileStream.Create( PEFileName ,fmCreate );
            try
              SaveStream.Seek(0, soFromBeginning);
              iGraphic.AppendToStream(SaveStream);
            finally
              SaveStream.Free;
            end;
          end
          Else IF LowerCase(ExtractFileExt(PEFileName)) = '.jpg' Then
          begin
            if FileExists( PEFileName ) then
              DeleteFile(PEFileName);
            //SaveStream := TFileStream.Create( PEFileName ,fmCreate );
            JpgGr := TJpegGraphic.Create;
            try
              JpgGr.Assign(iGraphic);
              JpgGr.SaveQuality := FJpgCompression;
              //JpgGr.AppendToStream(SaveStream);
              JpgGr.SaveToFile(PEFileName);
            finally
              JpgGr.Free;
              //SaveStream.Free;
            end;
          end;
          PageDone;
        end;
      end;
      if iGraphic = iGraphic_First then
        iGraphic := iGraphic_Sec
      else
        iGraphic.Assign(nil);
      //iGraphic.Assign(iGraphic_Sec);
    end;
  finally
  //iGraphic.Free;
  iGraphic_First.Free;
  iGraphic_Sec.Free;
  end;
 
end;
 
Procedure TCB_IMGPSScanX.R_W_Scanini(Mode:Char); //'R'讀取;'W'寫入
var
  ini : Tinifile;
begin
  ini := Tinifile.Create(ScaniniPath+'FBScan.ini');
  try
    case Mode of
    'R':begin
          DeviceDelete := ini.ReadBool('DeviceDelete','Mode',Def_DeviceDelete);
          DeviceDeleteSize := ini.ReadInteger('DeviceDelete','Size_New',Def_DeviceDeleteSize);
          ScannerReverse := ini.ReadBool('Scanner','Reverse',Def_ScannerReverse);
          BoardClear := ini.ReadBool('Scanner','BoardClear',Def_BoardClear);
          //ScanDpi := ini.ReadInteger('Scanner','Dpi',Def_ScanDpi);
          //ScanDuplex := ini.ReadBool('Scanner','Duplex',Def_ScanDuplex);
          ScanRotate := ini.ReadInteger('Scanner','ScanRotate',Def_ScanRotate);
          ScanDeskew := ini.ReadBool('Scanner','ScanDeskew',Def_ScanDeskew);
          ScanBright := ini.ReadInteger('Scanner','ScanBright',Def_ScanBright);
          ScanContrast := ini.ReadInteger('Scanner','ScanContrast',Def_ScanContrast);
          ScanImgShowMode := ini.ReadInteger('Scanner','ScanImgShowMode',Def_ScanImgShowMode);
          ScanImgSetUse := ini.ReadBool('Scanner','ScanImgSetUse',Def_ScanImgSetUse);  //20101110 BA說掃瞄器廠商有調設定要新增此選項是否啟動
        end;
    'W':begin
          ini.WriteBool('DeviceDelete','Mode',DeviceDelete);
          ini.WriteInteger('DeviceDelete','Size_New',DeviceDeleteSize);
          ini.WriteBool('Scanner','Reverse',ScannerReverse);
          ini.WriteBool('Scanner','BoardClear',BoardClear);
          //ini.ReadInteger('Scanner','Dpi',ScanDpi);
          //ini.WriteBool('Scanner','Duplex',ScanDuplex);
          ini.WriteInteger('Scanner','ScanRotate',ScanRotate);
          ini.WriteBool('Scanner','ScanDeskew',ScanDeskew);
          ini.WriteInteger('Scanner','ScanBright',ScanBright);
          ini.WriteInteger('Scanner','ScanContrast',ScanContrast);
          ini.WriteInteger('Scanner','ScanImgShowMode',ScanImgShowMode);
          ini.WriteBool('Scanner','ScanImgSetUse',ScanImgSetUse);  //20101110 BA說掃瞄器廠商有調設定要新增此選項是否啟動
        end;
    end;
 
  finally
  ini.Free;
  end;
end;
 
Procedure TCB_IMGPSScanX.GetDefScanIni; //取得掃瞄的預設值
var
  i : Integer;
  PARA_NO,PARA_CONTENT: String;
begin
  Def_DeviceDelete := True;
  Def_DeviceDeleteSize := 3072;  //20120821 改成3000(出現)
  Def_ScannerReverse := False;
  Def_BoardClear := False;
  Def_ScanDpi := 300;
  Def_ScanDuplex := True;
  Def_ScanRotate := 0;
  Def_ScanDeskew := False;
  Def_ScanImgSetUse := False;
  Def_ScanBright := 0;
  Def_ScanContrast := 0;
  Def_ScanImgShowMode := 2;
 
  for i := 0 to WORK_INF_List.Count - 1 do
  begin
    IF GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_BLANKDEL_USE' Then   //空白頁啟動
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if UpperCase(PARA_CONTENT) ='Y'  then
        Def_DeviceDelete := True
      Else
        Def_DeviceDelete := False;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_BLANKDEL_SIZE' Then  //空白頁Size
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if PARA_CONTENT = ''  then
        Def_DeviceDeleteSize := 0
      Else
        Def_DeviceDeleteSize := Strtoint(PARA_CONTENT);
 
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_REVERSE' Then  //是否需反相
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if UpperCase(PARA_CONTENT) ='Y'  then
        Def_ScannerReverse := True
      Else
        Def_ScannerReverse := False;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_BOARDCLEAR' Then  //是否清黑邊
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if UpperCase(PARA_CONTENT) ='Y'  then
        Def_BoardClear := True
      Else
        Def_BoardClear := False;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_DPI' Then  //掃瞄DPI
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if PARA_CONTENT = ''  then
        Def_ScanDpi := 300
      else
        Def_ScanDpi := Strtoint(PARA_CONTENT);
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_DUPLEX' Then  //是否雙面掃瞄
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if UpperCase(PARA_CONTENT) ='Y'  then
        Def_ScanDuplex := True
      Else
        Def_ScanDuplex := False;
 
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_ROTATE_MODE' Then //掃瞄時旋轉角度
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if PARA_CONTENT = '0' then
        Def_ScanRotate := 0
      Else if PARA_CONTENT = '1' then
        Def_ScanRotate := 270
      Else if PARA_CONTENT = '2' then
        Def_ScanRotate := 180
      Else if PARA_CONTENT = '3' then
        Def_ScanRotate := 90
      Else
        Def_ScanRotate := 0;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_DESKEW' Then //是否傾斜矯正
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if UpperCase(PARA_CONTENT) ='Y'  then
        Def_ScanDeskew := True
      Else
        Def_ScanDeskew := False;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_IMGSET_USE' Then  //是否使用亮度對比設定
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if UpperCase(PARA_CONTENT) ='Y'  then
        Def_ScanImgSetUse := True
      else
        Def_ScanImgSetUse := False;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_BRIGHT' Then //亮度
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if PARA_CONTENT =''  then
        Def_ScanBright := 0
      Else
        Def_ScanBright := strtoint(PARA_CONTENT);
      if (Def_ScanBright > 255) or (Def_ScanBright < -255) then
        Def_ScanBright := 0;
 
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_CONTRAST' Then //對比
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if PARA_CONTENT =''  then
        Def_ScanContrast := 0
      Else
        Def_ScanContrast := strtoint(PARA_CONTENT);
      if (Def_ScanContrast > 255) or (Def_ScanContrast < -255) then
        Def_ScanContrast := 0;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_SHOW_MODE' Then
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      if PARA_CONTENT = '0' then
        Def_ScanImgShowMode := 0
      Else if PARA_CONTENT = '1' then
        Def_ScanImgShowMode := 1
      Else if PARA_CONTENT = '2' then
        Def_ScanImgShowMode := 2
      Else
        Def_ScanImgShowMode := 0;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'CASE_IN_TIME' Then     //取進件截止時間
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      ScanDenialTime := PARA_CONTENT;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'SCAN_HINT' Then     //掃描提示字串
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      ScanDenialHint := PARA_CONTENT;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'NO_SAVE_FORM_ID' Then     //掃描不存檔之表單代號
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      NoSaveBarCodeList.CommaText := PARA_CONTENT;
    end
    Else if GetSQLData(WORK_INF_List,'PARA_NO',i) = 'LOCAL_PATH' Then     //本機端路徑
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      ImagePath:= PARA_CONTENT;
    end
    Else if UpperCase(GetSQLData(WORK_INF_List,'PARA_NO',i)) = 'GUIDEFORMID' Then     //當導引頁的表單
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      GuideFormIDList.CommaText := PARA_CONTENT;
    end
    Else if UpperCase(GetSQLData(WORK_INF_List,'PARA_NO',i)) = 'DIVPAGEFORMID' Then     //當分案頁的表單
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      DivPageFormIDList.CommaText := PARA_CONTENT;
    end
    Else if UpperCase(GetSQLData(WORK_INF_List,'PARA_NO',i)) = 'FILE_COMPRESSION' Then     //20171211 jpg to tif 壓縮比
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      FJpgCompression := StrToInt(PARA_CONTENT);
    end
    Else if UpperCase(GetSQLData(WORK_INF_List,'PARA_NO',i)) = 'MAX_UPLOAD_SIZE' Then     //取得 上傳大小的限制(MB)
    begin
      PARA_CONTENT := GetSQLData(WORK_INF_List,'PARA_CONTENT',i);
      FMaxUploadSize := PARA_CONTENT;
    end;
  end;
  ScanDuplex := Def_ScanDuplex;
end;
 
procedure TCB_IMGPSScanX.EnableImage(v:integer;Sender : TObject);
var bmp : Tbitmap;
begin
  DesableImage;
  bmp := TBitmap.Create;
  try
    ImageList3.GetBitmap(v,bmp);
    TBitBtn(Sender).Glyph.Assign(bmp);
  finally
  bmp.Free;
  end;
  ViewMouseMode(v);
end;
 
procedure TCB_IMGPSScanX.DesableImage;
var bmp : Tbitmap;
    i : integer;
begin
  NowClick := -1;
  bmp := Tbitmap.Create;
  try
  For i:= 0 to 6 do
  begin
    ImageList4.GetBitmap(i,bmp);
    TBitBtn(FindComponent('FC'+IntToStr(i))).Glyph.Assign(bmp);
    bmp.Width:=0;
    bmp.Handle:=0;
  end;
  finally
  bmp.Free;
  end;
  ViewMouseMode(NowClick);
end;
 
Procedure TCB_IMGPSScanX.DeleteImageFile(Path,FileName,CaseID:String); // 刪除檔案  (無法得到DocDir用)
var
  i : Integer;
  FileList:TStringlist;
  DocDir : String;
begin
  
 
  DeleteFile(Path+FileName);
  DocDir := Path2DocDir(Path,CaseID);
//ShowMessage('DocDir='+DocDir);
  SetContextList('D',-1,CaseID,DocDir,FileName);
  {FileList:=TStringlist.Create;
  try
    if FileExists(Path+'Context.dat') then
      FileList.LoadFromFile(Path+'Context.dat');
    for i := 0 to FileList.Count - 1 do
    begin
      if FileName = FileList.Strings[i] then
      begin
        FileList.Delete(i);
        FileList.SaveToFile(Path+'Context.dat');
        Break;
      end;
    end;
    if FileList.Count = 0 then
      DeleteFile(Path+'Context.dat');
  finally
  FileList.Free;
  end;}
end;
 
Function TCB_IMGPSScanX.DeleteDocNoFileForESCAN(Path,DocNo:String):Boolean;  //刪除指定DocNo文件
var
  i,j,k: Integer;
  FName : String;
  ST1,ST2,ST3:TStringList;
 
begin
 
  Result := False;
//ShowMessage(DocNo);
  for i := ContextList.Count - 1 downto 0 do
  begin
    FName := ContextList.Strings[i];
    If (DocNo = FormCode2DocNo(FileName2FormCode(FName))) or (DocNo=AttName) then
    begin
      if not ISExistImg(Path+'\'+FName) then
      begin
        DeleteFile(Path+'\'+FName);
        ContextList.Delete(i);
      end;
      Result := True; //有刪到指定文件
    end;
  end;
  ContextList.SaveToFile(Path+'\Context.dat');
  ContextList.LoadFromFile(Path+'\Context.dat');
 
  if ContextList.Count=0 then
  begin
    _DelTree(Path);
    SetDocNoList('D',-1,NowCaseNo,NowDocDir,'');
  end;
 
end;
 
Function TCB_IMGPSScanX.DownLoadImage(Path,CaseID:String):Boolean;
begin
  Result := True;
  if not GetftpInfo(CaseID,'download') then   //取案件下載方式
  begin
    DownFileErrStr := _Msg('取案件下載資訊失敗,')+HttpErrStr;
    Result := False;
    Exit;
  end;
  case TransMode of
    tsHttp:
    begin
      ShowText := _Msg('案件下載中(Http),請稍候');
      DataLoading(True,True);
      If not Down_Img(ImageSavePath+FCaseID+'\Download\',FCaseID) then
      begin
        Showmessage(FCaseID+_msg('載入異動影像時,網路發生錯誤')+HttpErrStr);
        DataLoading(False,False);
        Exit;
      end;
    end;
    tsFtp:
    begin
      ShowText := _Msg('案件下載中(Ftp),請稍候');
      DataLoading(True,True);
      SetFtpInfo;
 
      if not IIS_Ftp.FtpsConnect then
      begin
        DownFileErrStr := Format(_Msg('無法連上Ftp主機,錯誤原因:%s')+#13+'%s',[FtpErrReason,FTPSClient1.LastReceivedReply]);
        Result := False;
        Exit;
      end;
      if not IIS_Ftp.FtpsDownloadFile(FFtpExtraPath,CaseID+'.zip',Path+CaseID+'.zip',display1) then
      begin
        DownFileErrStr := Format(_Msg('錯誤原因:%s'),[FtpErrStr]);
        Result := False;
        Exit;
      end;
      ExecuteUnZip(Path+CaseID+'.zip',Path,False);
      DeleteFile(Path+CaseID+'.zip');
    end;
  end;
end;
 
 
Procedure TCB_IMGPSScanX.CaseResort2Scanlist(Path:String); //案件的檔案重新排序給scanlist(次文件依FormID排)
var
  i,n,v,v1 : Integer;
  S,S1 : TStringlist;
  FormID,OldName,NewName,DocNo,Doc_Type:String;
  x : Integer;
begin
  S := TStringlist.Create;
  S1 := TStringlist.Create;
  try
  if FileExists(Path+'Context.dat') then
    S.LoadFromFile(Path+'Context.dat');
  X := 0;
  for I := 1 to FORM_INF_List.Count - 1 do    //在FormID有設定的   //主文件 照SQL排   20101028改
  begin
    FormID := GetSQLData(FORM_INF_List,'T1.FORM_ID',i);
    if FormCode2FileName(FormID,S) = '' then
       Continue;
    Doc_Type := GetSQLData(FORM_INF_List,'T2.DOC_TYPE',i);
    for n := 0 to S.Count - 1 do
    begin
      if (S.Strings[n][1] <> '*') and (FileName2FormCode(S.Strings[n]) = FormID) and (Doc_Type='1') then
      begin
        Inc(X);
        OldName := S.Strings[n];
        //NewName := Add_Zoo(S.Count+x,3)+Copy(OldName,4,length(OldName)-3); //從原有數量加1開始編
        NewName := Add_Zoo(S.Count+x,3)+FileName2NoQuene_Filename(OldName); //從原有數量加1開始編
        S.Strings[n] := '*'+S.Strings[n];
        S1.Add(OldName+','+NewName);
      end;
    end;
  end;
 
  for I := 0 to FORM_INF_List.Count - 1 do  //次文件 照SQL排   20110512為了某個文件要先打的原因要求改
  begin
 
    FormID := GetSQLData(FORM_INF_List,'T1.FORM_ID',i);
    if FormCode2FileName(FormID,S) = '' then
       Continue;
    Doc_Type := GetSQLData(FORM_INF_List,'T2.DOC_TYPE',i);
    for n := 0 to S.Count - 1 do
    begin
      if (S.Strings[n][1] <> '*') and (FileName2FormCode(S.Strings[n]) = FormID) and (Doc_Type='2') then
      begin
        Inc(X);
        OldName := S.Strings[n];
        //NewName := Add_Zoo(S.Count+x,3)+Copy(OldName,4,length(OldName)-3); //從原有數量加1開始編
        NewName := Add_Zoo(S.Count+x,3)+FileName2NoQuene_Filename(OldName); //從原有數量加1開始編
        S.Strings[n] := '*'+S.Strings[n];
        S1.Add(OldName+','+NewName);
      end;
    end;
  end;
 
  {for I := 0 to Doc_Inf_List.Count - 1 do  //次文件 照文件代碼+掃瞄順序排   20101101改   20110512晚上又說改回來
  begin
    DocNo := GetSQLData(Doc_Inf_List,'DOC_NO',i);
    Doc_Type := GetSQLData(Doc_Inf_List,'DOC_TYPE',i);
    for n := 0 to S.Count - 1 do
    begin
      if (S.Strings[n][1] <> '*') and (FormCode2DocNo(FileName2FormCode(S.Strings[n])) = DocNo) and (Doc_Type='2') then
      begin
        Inc(X);
        OldName := S.Strings[n];
        NewName := Add_Zoo(S.Count+x,3)+Copy(OldName,4,length(OldName)-3); //從原有數量加1開始編
        S.Strings[n] := '*'+S.Strings[n];
        S1.Add(OldName+','+NewName);
      end;
    end;
  end;}
 
  {for n := 0 to S.Count - 1 do    //次文件 照掃瞄順序排   20101028改
  begin
    for i := 0 to FORM_INF_List.Count - 1 do
    begin
      FormID := GetSQLData(FORM_INF_List,'T1.FORM_ID',i);
      Doc_Type := GetSQLData(FORM_INF_List,'T2.DOC_TYPE',i);
      if (S.Strings[n][1] <> '*') and (FileName2FormCode(S.Strings[n]) = FormID) and (Doc_Type='2') then
      begin
        Inc(X);
        OldName := S.Strings[n];
        NewName := Add_Zoo(S.Count+x,3)+Copy(OldName,4,length(OldName)-3); //從原有數量加1開始編
        S.Strings[n] := '*'+S.Strings[n];
        S1.Add(OldName+','+NewName);
      end;
    end;
  end;}
  for i := 0 to S.Count - 1 do   //FormID沒設定的或附件
  begin
    if S.Strings[i][1] <> '*' then
    begin
      Inc(X);
      OldName := S.Strings[i];
      //NewName := Add_Zoo(S.Count+x,3)+Copy(OldName,4,length(OldName)-3);
      NewName := Add_Zoo(S.Count+x,3)+FileName2NoQuene_Filename(OldName);
      S.Strings[i] := '*'+S.Strings[i];
      S1.Add(OldName+','+NewName);
    end;
  end;
 
  S.Clear;
  for i := 0 to S1.Count - 1 do  //開始轉換檔名
  begin
    v := Pos(',',S1.Strings[i]);
    v1 := length(S1.Strings[i]);
    OldName := copy(S1.Strings[i],1,v-1);
    NewName := copy(S1.Strings[i],v+1,v1-v);
    //if FileExists(Path+OldName) then
    //begin
      //ReNameFile(Path+OldName,Path+NewName);
      S.Add(NewName);
      S.SaveToFile(Path+'scanlist.dat');
    //end;
  end;
  ReSortFileName2Scanlist(Path);
  finally
  S.Free;
  S1.Free;
  end;
end;
 
 
Procedure TCB_IMGPSScanX.GetSelectImageFile;
var
  i : Integer;
  FormID,FormName,DocNo : String;
  PreNode2Name : String;
  iFormID : String;
  iISBName : String;
  iISB : TImageScrollBox;
begin
  NowSelectFileList.Clear;
  for i := 0 to ComponentCount -1 do
  begin
    if (Components[i] is TShape) and (copy(Components[i].Name,1,2)='SP') then
    begin
      iISBName := ShapeName2PreViewISBName(TShape(Components[i]));
      iISB := TImageScrollBox(FindComponent(iISBName));
      NowSelectFileList.Add(iISB.FileName);
    end;
  end;
end;
 
Procedure TCB_IMGPSScanX.ParserPoint(S:String); //解析十字點的字串
var
  PointList : TStringlist;
  Rect : TRect;
begin
  PointList := TStringlist.Create;
  try
    PointList.Text := S;
    IF PointList.Count <> 6 Then
    begin
      UpLPoint := Str2Point('0,0');
      UpRPoint := Str2Point('0,0');
      DownLPoint := Str2Point('0,0');
      DownRPoint := Str2Point('0,0');
      Point_Width := '0';
      Point_Height := '0';
    end
    Else
    begin
      UpLPoint := Str2Point(PointList[0]);
      DownLPoint := Str2Point(PointList[1]);
      UpRPoint := Str2Point(PointList[2]);
      DownRPoint := Str2Point(PointList[3]);
      Point_Width := PointList[4];
      Point_Height := PointList[5];
    end;
  finally
  PointList.Free;
  end;
end;
 
Function TCB_IMGPSScanX.CheckScanDenialTime:Boolean;
Var
  NowTime : String;
begin
  NowTime := GetBalance2Time(Balance);
  NowTime := Copy(NowTime,1,2)+':'+Copy(NowTime,3,2)+':'+Copy(NowTime,5,2);
  Result := True;
  if ScanDenialTime <> '' then
  begin
    if StrtoTime(NowTime) >= StrtoTime(ScanDenialTime) then
      Result := False;
  end;
end;
 
procedure TCB_IMGPSScanX.initkscan;
begin
  ScanDuplexCB.Enabled := False;
  if Scanner.IsConfigured then
  begin
    try
      Scanner.OpenSource;
      IF Scanner.DuplexCap > 0 Then
      begin
        ScanDuplexCB.Enabled := True;
      end;
      {IF Scanner.FEEDERCAP Then
        ScanFlatCB.Enabled := True; }
    Except
      DataLoading(False,True);
      Exit;
    end;
    Scanner.CloseSource;
  end;
end;
 
Function TCB_IMGPSScanX.CheckNeedCrop(Graphic:TDibGraphic):Boolean; //是否是A3要切影像
Var
  i,FormIDCount : Integer;
begin
  Result := False;
  FormIDCount := 0;
  if (Graphic.Width > (4 * Graphic.XDotsPerInch)) {or (Graphic.Height > (15 * Graphic.YDotsPerInch))} then
  //if (Graphic.Width > (6 * Graphic.XDotsPerInch)) then
  begin
    for I := 1 to MpsBarcodeinf.Count do
    begin
      if (Length(MpsBarcodeinf.Text[i])=FormIDLength) and FormIDExists(MpsBarcodeinf.Text[i],False,0) then
      begin
        inc(FormIDCount);
      end;
    end;
 
  end;
 
//ShowMessage('FormIDCount='+IntToStr(FormIDCount)+#10#13+'MpsBarcodeinf.count='+IntToStr(MpsBarcodeinf.count));
  if FormIDCount = 2 then
  begin
    Result := True;
  end;
end;
 
Procedure TCB_IMGPSScanX.MoveImage(Path:String;mp:Integer); //移動頁數
var
  i,n,inx:Integer;
  FList,D_Flist:TStringlist;
begin
  FList := TStringlist.Create;
  D_Flist := TStringlist.Create;
  try
    FList.LoadFromFile(Path+'Context.dat');
    //Showmessage(Path);
    //Showmessage(Flist.Text);
    for i := 0 to FList.Count - 1 do
    begin
      Renamefile(Path+Flist.Strings[i],path+'@'+Flist.Strings[i]);
      Flist.Strings[i]:= '@'+Flist.Strings[i];
    end;
 
    for i := 0 to ComponentCount -1 do
    begin
      if (Components[i] is TShape) and (copy(Components[i].Name,1,2)='SP') then
      begin
        inx := strtoint(Copy(TShape(Components[i]).Name,3,length(TShape(Components[i]).Name)-2));
        D_Flist.Add(Flist.Strings[inx-1]);
        //Renamefile(Path+Flist.Strings[inx-1],path+'@'+Flist.Strings[inx-1]);
      end;
    end;
    //Showmessage('aa');
    for i := 0 to D_Flist.Count -1 do
    begin
      for n := 0 to FList.Count - 1 do
      begin
        //if Flist.Strings[n]=StringReplace(D_Flist.Strings[i],'@','',[rfReplaceAll]) then
        if Flist.Strings[n]=D_Flist.Strings[i] then
        begin
          Flist.Delete(n);
          Break;
        end;
      end;
    end;
    //Showmessage('bb');
    for i := 0 to D_Flist.Count - 1 do
    begin
      Flist.Insert(mp-1+i,D_Flist.Strings[i]);
    end;
 
    Flist.SaveToFile(Path+'Context.dat');
    //Showmessage(Flist.Text);
    //Showmessage('CC');
    ReSortFileName(Path);
    TreeView1click(self);
 
 
  finally
  FList.Free;
  D_Flist.Free;
  end;
end;
 
Function TCB_IMGPSScanX.FileName2ScanPage(FileName:String):Integer; //從檔名轉出掃瞄頁數
Var
  v : Integer;
  FName : String;
begin
  FName := ExtractFileName(FileName);
  v := Pos('_',FName);
  if v = 0 then   //附件
    v := pos('.',FName);
  Result := Strtoint(Copy(FName,1,v-1));
end;
 
Procedure TCB_IMGPSScanX.ReSortFileName2Scanlist(Path:String); //檔名重新排序給Scanlist.dat
var
  i : Integer;
  OldName,NewName : String;
  S : TStringlist;
begin
  S := TStringlist.Create;
  try
    if FileExists(Path+'scanlist.dat') then
      S.LoadFromFile(Path+'scanlist.dat');
    for i := 0 to S.Count - 1 do
    begin
      OldName := S.Strings[i];
      //NewName := Add_Zoo(i+1,3)+Copy(OldName,4,length(OldName)-3);
      NewName := Add_Zoo(i+1,3)+FileName2NoQuene_Filename(OldName);
      //ReNameFile(Path+OldName,Path+NewName);
      S.Strings[i] := NewName;
    end;
    S.SaveToFile(Path+'scanlist.dat');
  finally
  S.Free;
  end;
end;
 
 
function TCB_IMGPSScanX.Get_imgdpi: WideString;
begin
 
end;
 
function TCB_IMGPSScanX.Get_scancolor: WideString;
begin
 
end;
 
procedure TCB_IMGPSScanX.Set_imgdpi(const Value: WideString);
begin
  if Value ='' then
  begin
    FImgDPI := 300;
    ScanDpi := FImgDPI;
  end
  else
  begin
    FImgDPI := StrToInt(Value);
    ScanDpi := FImgDPI;
    Def_ScanDpi := FImgDPI;
  end;
 
end;
 
procedure TCB_IMGPSScanX.Set_scancolor(const Value: WideString);
begin
  if value='' then
  begin
    FScanColor := 0;
    ScanColor := ifBlackWhite;
  end
  else
  begin
    FScanColor := StrToInt(Value);
    ScanColor := ifBlackWhite;
  end;
 
  if FScanColor = 1 then
  begin
    ScanColor := ifGray256 ;
  end;
 
  if FScanColor = 2 then
  begin
    ScanColor := ifTrueColor ;
  end;
 
end;