-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeviceImage.as
784 lines (656 loc) · 19.2 KB
/
DeviceImage.as
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
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.JPEGEncoderOptions;
import flash.display.Loader;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.MediaEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.CameraRoll;
import flash.media.CameraRollBrowseOptions;
import flash.media.CameraUI;
import flash.media.MediaPromise;
import flash.media.MediaType;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
import flash.utils.IDataInput;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.setTimeout;
import jp.shichiseki.exif.ExifInfo;
import jp.shichiseki.exif.IFD;
import nativeClasses.cameraUI.DistriqtCameraUI;
import videoShow.VideoClass;
import videoShow.VideoEvents;
import nativeClasses.distriqtCameraRoll.DistriqtCameraRoll;
import flash.permissions.PermissionStatus;
import flash.events.PermissionEvent;
public class DeviceImage
{
private static var onDone:Function ;
/**Device images are always in jpeg*/
public static var imageBytes:ByteArray ;
/**Video bytes*/
public static var videoBytes:ByteArray ;
/**This is the location of the video to save for capturing demo*/
private static var videoTempFile:File ;
/**This will help to load videoPreview for demoImage*/
private static var videoDemoLoader:VideoClass ;
/**This will save the video camera status*/
private static var onLoadingVideo:Boolean ;
/**This is the loaded image bitmap data*/
public static var imageBitmapData:BitmapData ;
private static var camera:CameraUI,
dataSource:IDataInput,
cameraRoll:CameraRoll;
/**Below variables will uses when user requests ResizeFunction*/
private static var W:Number,H:Number;
/**uses to load image for resizing*/
private static var loader:Loader;
public static var autoResize:Boolean = true ;
private static var tempW:Number,tempH:Number;
/**1: //normal<br>
3 //rotated 180 degrees (upside down)<br>
6: //rotated 90 degrees CW<br>
8: //rotated 90 degrees CCW<br>
9: //unknown<br>
*/
private static var currentImageOriented:uint;
/**flash.events.PermissionEvent*/
private static var PermissionEventClass:Class;
/**flash.permissions.PermissionStatus*/
private static var PermissionStatusClass:Class;
private static function setUpPermissionClasses():void
{
try
{
PermissionEventClass = getDefinitionByName("flash.events.PermissionEvent") as Class;
PermissionStatusClass = getDefinitionByName("flash.permissions.PermissionStatus") as Class;
}
catch(e)
{
PermissionEventClass = null ;
PermissionStatusClass = null ;
}
}
public static function get isSupported():Boolean
{
return CameraUI.isSupported;
}
/**No image selected*/
private static function mediaLoadingCanseled(e:Event):void
{
videoBytes = null ;
imageBytes==null;
imageBitmapData = null;
onDone();
}
public static function loadVideo(onVideoReady:Function,videoFileTarget:File):void
{
onLoadingVideo = true ;
imageBytes = null;
imageBitmapData = null ;
onDone = onVideoReady ;
tempW = NaN;
tempH = NaN ;
loadThisImageToSend(videoFileTarget);
}
/**Open the video camera*/
public static function getVideoCamera(onVideoReady:Function):void
{
onLoadingVideo = true ;
imageBytes = null;
imageBitmapData = null ;
onDone = onVideoReady ;
if(DistriqtCameraUI.isSupport())
{
trace("Distriqt Camera is Supporting");
DistriqtCameraUI.captureVideo(function():void{
trace("** Camera file loaded on DeviceImage **");
videoBytes = DistriqtCameraUI.fileByte ;
trace("*** The video size is : "+videoBytes.length);
onDone();
});
return ;
}
else
{
trace("*** Distriqt is not supporting***");
}
if(!CameraUI.isSupported)
{
onDone()
return ;
}
tempW = NaN;
tempH = NaN ;
camera = new CameraUI();
camera.addEventListener(MediaEvent.COMPLETE,sendCameraImage);
camera.addEventListener(Event.CANCEL,mediaLoadingCanseled);
if (PermissionEventClass!=null && (Camera as Object).permissionStatus != (PermissionStatusClass as Object).GRANTED)
{
camera.addEventListener((PermissionEventClass as Object).PERMISSION_STATUS,
function(e:*):void {
if (e.status == (PermissionStatusClass as Object).GRANTED)
{
launchVideo();
}
else
{
trace("Use had no permission to open camera");
return ;
}
});
try {
(camera as Object).requestPermission();
} catch(e:Error)
{
trace("another request is in progress");
return ;
}
}
else
{
launchVideo()
}
}
private static function launchVideo():void
{
camera.launch(MediaType.VIDEO);
}
/**When the image is ready, you can get its image from imageBytes*/
public static function getCameraImage(onImageReady:Function,imageW:Number=NaN,imageH:Number=NaN,loadThisFileIfNotSupporting:String=null):void
{
onLoadingVideo = false ;
imageBytes = null;
imageBitmapData = null ;
if(!CameraUI.isSupported)
{
loadFile(onImageReady,new File(loadThisFileIfNotSupporting),imageW,imageH);
return ;
}
tempW = imageW;
tempH = imageH ;
onDone = onImageReady ;
camera = new CameraUI();
camera.addEventListener(MediaEvent.COMPLETE,sendCameraImage);
camera.addEventListener(Event.CANCEL,mediaLoadingCanseled);
setUpPermissionClasses();
if (PermissionEventClass!=null && (Camera as Object).permissionStatus != (PermissionStatusClass as Object).GRANTED)
{
camera.addEventListener((PermissionEventClass as Object).PERMISSION_STATUS,
function(e:*):void {
if (e.status == (PermissionStatusClass as Object).GRANTED)
{
launchImage();
}
else
{
trace("Use had no permission to open camera");
return ;
}
});
try {
(camera as Object).requestPermission();
} catch(e:Error)
{
trace("another request is in progress");
return ;
}
}
else
{
launchImage()
}
}
private static function launchImage():void
{
camera.launch(MediaType.IMAGE);
}
protected static function sendCameraImage(ev:MediaEvent):void
{
if(ev.data.file!=null)
{
loadThisImageToSend(ev.data.file) ;
}
else
{
dataSource = ev.data.open();
if(ev.data.isAsync)
{
var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, readMediaData );
}
else
{
readMediaData();
}
}
}
private static function readMediaData(e:*=null):void
{
imageBytes = new ByteArray();
dataSource.readBytes(imageBytes);
imageBytes.position = 0 ;
if( !onLoadingVideo && (autoResize || !isNaN(tempW)))
{
resizeLoadedImage(onDone,tempW,tempH,imageBytes);
}
else
{
onDone();
}
}
public static function loadFile(onImageReady:Function,file:File,imageW:Number=NaN,imageH:Number=NaN):void
{
onLoadingVideo = false ;
tempW = imageW;
tempH = imageH ;
onDone = onImageReady ;
loadThisImageToSend(file);
}
private static function loadThisImageToSend(file:File):void
{
var fileStream:FileStream = new FileStream();
fileStream.addEventListener(Event.COMPLETE, imageFileLoaded);
fileStream.openAsync(file,FileMode.READ);
}
private static function imageFileLoaded(event:Event):void {
var fileStream:FileStream = event.currentTarget as FileStream;
if(!onLoadingVideo)
{
imageBytes = new ByteArray();
fileStream.readBytes(imageBytes,0,fileStream.bytesAvailable);
imageBytes.position = 0 ;
}
else
{
videoBytes = new ByteArray();
fileStream.readBytes(videoBytes,0,fileStream.bytesAvailable);
videoBytes.position = 0 ;
}
fileStream.close();
if( !onLoadingVideo && (autoResize || !isNaN(tempW)))
{
resizeLoadedImage(onDone,tempW,tempH,imageBytes);
}
else if(false && onLoadingVideo)//Capturing video had problem
{
captureVideoDemo(onDone);
}
else
{
onDone();
}
}
public static function captureVideoDemo(OnDone:Function, VideoBytes:ByteArray=null):void
{
onDone = onDone ;
if(VideoBytes!=null)
{
videoBytes = VideoBytes ;
}
if(videoDemoLoader)
{
videoDemoLoader.unLoad();
}
if(DevicePrefrence.isIOS())
{
imageBitmapData = new BitmapData(200,200,false,0x222222);
imageBytes = BitmapEffects.createJPG(imageBitmapData);
OnDone();
return ;
}
videoDemoLoader = new VideoClass();
if(videoTempFile!=null && videoTempFile.exists)
{
videoTempFile.deleteFileAsync();
}
videoTempFile = File.createTempFile() ;
FileManager.seveFile(videoTempFile,videoBytes,true,onVideoSavedToHard);
}
private static function onVideoSavedToHard():void
{
trace("Video file is saved to load its image");
videoDemoLoader.addEventListener(VideoEvents.VIDEO_LOADED,videoDemoIsLoaded);
videoDemoLoader.loadThiwVideo(videoTempFile.url,false);
}
protected static function videoDemoIsLoaded(event:Event):void
{
trace("Video is playing to get captured");
videoDemoLoader.removeEventListener(VideoEvents.VIDEO_LOADED,videoDemoIsLoaded);
videoDemoLoader.pause();
videoDemoLoader.seek = 0.5;
videoDemoLoader.addEventListener(VideoEvents.VIDEO_STATUS_CHANGED,videoGosToSelectedFrame);
}
protected static function videoGosToSelectedFrame(event:Event):void
{
trace("Seek changed");
videoDemoLoader.removeEventListener(VideoEvents.VIDEO_STATUS_CHANGED,videoGosToSelectedFrame);
videoDemoLoader.pause();
setTimeout(captureVideo,1000);
}
private static function captureVideo():void
{
trace("Capture the video frame : "+videoDemoLoader.seek+' > '+videoDemoLoader.width,videoDemoLoader.height);
imageBitmapData = new BitmapData(videoDemoLoader.width,videoDemoLoader.height,false,0xffffff);
imageBitmapData.draw(videoDemoLoader.videoObject);
trace("Now what??");
//videoDemoLoader.unLoad();
//videoDemoLoader = null ;
trace("remove vvideo??");
imageBytes = BitmapEffects.createJPG(imageBitmapData);
onDone()
}
/////////////////////////////////////////////////////////////////
/**Returns true if videoGallery is reachable*/
public static function isVideoFromGallerySupports():Boolean
{
return VideoManaerAsist.isSupports();
}
/**Load a video from the video gallery*/
public static function loadVideoFromGallery(onImageLoaded:Function,loadThisFileIfOnPC:File=null):void
{
onLoadingVideo = true ;
imageBitmapData = null;
imageBytes = null ;
onDone = onImageLoaded ;
if(DistriqtCameraRoll.isSupport())
{
DistriqtCameraRoll.loadVideoFromGallery();
}
else if(VideoManaerAsist.isSupports())
{
VideoManaerAsist.loadVideoFromGallery(onVideoLoaderFromGalleryByVideoManager);
}
else if(DevicePrefrence.isItPC && loadThisFileIfOnPC!=null)
{
videoBytes = FileManager.loadFile(loadThisFileIfOnPC);
onDone();
}
}
private static function onVideoLoadedFromDistriqtCameraRoll():void
{
//TODO
}
/**Video loaded from VideoManagerAsist*/
private static function onVideoLoaderFromGalleryByVideoManager():void
{
trace("Video raw path is : "+VideoManaerAsist.rawPath);
videoBytes = FileManager.loadFile(new File(VideoManaerAsist.rawPath));
onDone();
}
/**Load image from file then it will call your function*/
public static function loadImageFromGallery(onImageLoaded:Function,rect:Rectangle=null,imageW:Number=NaN,imageH:Number=NaN,loadThisFileIfNotSupporting:String=null):void
{
onLoadingVideo = false ;
imageBitmapData = null;
imageBytes = null ;
tempW = imageW;
tempH = imageH ;
if(!CameraRoll.supportsBrowseForImage)
{
loadFile(onImageLoaded,new File(loadThisFileIfNotSupporting),imageW,imageH);
return ;
}
onDone = onImageLoaded ;
cameraRoll = new CameraRoll();
if(rect!=null)
{
browsOptions = new CameraRollBrowseOptions();
browsOptions.origin = rect.clone() ;
}
cameraRoll.addEventListener(MediaEvent.SELECT,addThisImageTo);
cameraRoll.addEventListener(Event.CANCEL,mediaLoadingCanseled);
getCameraRollPermission(launchCameraRoll);
}
public static function getCameraRollPermission(onPermited:Function):void
{
var cameraRollPermission:CameraRoll = new CameraRoll();
if (CameraRoll.permissionStatus != PermissionStatus.GRANTED)
{
cameraRollPermission.addEventListener(PermissionEvent.PERMISSION_STATUS,
function(e:*):void {
if (e.status == PermissionStatus.GRANTED)
{
onPermited();
}
else
{
trace("Use had no permission to open camera");
return ;
}
});
try {
cameraRollPermission.requestPermission();
} catch(e:Error)
{
trace("another request is in progress");
return ;
}
}
else
{
onPermited()
}
}
private static function launchCameraRoll():void
{
cameraRoll.browseForImage(browsOptions);
}
private static function addThisImageTo(ev:MediaEvent):void
{
var media:MediaPromise = ev.data ;
if(media.file!=null)
{
loadThisImageToSend(media.file);
}
else
{
dataSource = media.open();
if(media.isAsync)
{
var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, readMediaData );
}
else
{
readMediaData();
}
}
}
////////////////////////////////Resize funciton
/**This function can also uses to convert byteArray to BitmapData in requseted size*/
public static function resizeLoadedImage(onResized:Function,newWidth:Number=NaN,newHeight:Number=NaN,fileBytes:ByteArray=null,forceToResize:Boolean=true):void
{
onLoadingVideo = false ;
onDone = onResized ;
if(fileBytes == null && imageBytes==null)
{
trace(new Error("No image loaded yet"));
onDone();
return ;
}
else
{
if(fileBytes == null)
{
fileBytes = imageBytes ;
}
}
imageBytes = fileBytes ;
currentImageOriented = getOrientation(fileBytes);
W = newWidth;
H = newHeight ;
if(forceToResize && isNaN(H))
{
H = 768;
}
if(forceToResize && isNaN(W))
{
W = 1024;
}
loader = new Loader();
var loaderContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true ;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,fileLoaderToResize);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,fileLoaderError);
loader.loadBytes(fileBytes,loaderContext);
}
/**1: //normal<br>
3 //rotated 180 degrees (upside down)<br>
6: //rotated 90 degrees CW<br>
8: //rotated 90 degrees CCW<br>
9: //unknown<br>
*/
public static function getOrientation(ImageBytes:ByteArray):uint
{
var exif:ExifInfo = new ExifInfo(ImageBytes);
if(exif.ifds != null)
{
var ifd:IFD = exif.ifds.primary ;
var str:String = "";
for (var entry:String in ifd) {
if(entry == "Orientation"){
str = ifd[entry];
break;
}
}
return uint(str);
}
else
{
return 9 ;
}
}
protected static function fileLoaderError(event:IOErrorEvent):void
{
trace(new Error("File cannot load"));
}
protected static function fileLoaderToResize(event:Event):void
{
trace("Image loaded to resize");
var bitmap:Bitmap = loader.content as Bitmap ;
var resizedPhoto:BitmapData ;
trace("currentImageOriented : "+currentImageOriented);
var rotateDegree:Number = 0 ;
switch(currentImageOriented)
{
case 3:
{
rotateDegree = 180 ;
break;
}
case 6:
{
rotateDegree = 90 ;
break;
}
case 8:
{
rotateDegree = -90 ;
break;
}
}
if(rotateDegree!=0)
{
resizedPhoto = BitmapEffects.rotateBitmapData(bitmap.bitmapData,rotateDegree);
}
if(!isNaN(W) && !isNaN(H) && ( bitmap.width>W || bitmap.height>H ))
{
if(resizedPhoto==null)
{
resizedPhoto = bitmap.bitmapData ;
}
resizedPhoto = BitmapEffects.changeSize(resizedPhoto,W,H,true,true,false);
}
else
{
imageBitmapData = bitmap.bitmapData.clone() ;
}
if(resizedPhoto!=null)
{
imageBytes = resizedPhoto.encode(resizedPhoto.rect,new JPEGEncoderOptions(70));
imageBitmapData = resizedPhoto.clone() ;
}
trace("Stored bitmapData is : "+imageBitmapData);
onDone();
}
//////////////////////////////////////////////////////Save image
private static var lastBitmap:Bitmap ;
private static var imageLoder:Loader ;
private static var cashedOnDone:Function ;
private static var browsOptions:CameraRollBrowseOptions;
/**file can be BitmapData, ByteArray or URL in string */
public static function saveImageToGallery(file:*, onImageSaved:Function):void
{
onLoadingVideo = false ;
cashedOnDone = onDone = onImageSaved ;
if(file is BitmapData)
{
lastBitmap = new Bitmap(file) ;
getCameraRollPermission(saveToGall);
}
else
{
createBitmapFromByteOrURL(file,function():void{getCameraRollPermission(saveToGall)});
}
}
private static function saveToGall():void
{
if(CameraRoll.supportsAddBitmapData)
{
cameraRoll = new CameraRoll();
cameraRoll.addBitmapData(lastBitmap.bitmapData);
}
else
{
trace("save not supports");
}
cashedOnDone();
}
private static function createBitmapFromByteOrURL(file:*,onCreated:Function):void
{
//trace("save this iamge : "+file);
trace(" > "+getQualifiedClassName(file));
onDone = onCreated ;
imageLoder = new Loader();
var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
context.allowLoadBytesCodeExecution = true ;
imageLoder.contentLoaderInfo.addEventListener(Event.COMPLETE,fileIsReady);
if(file is ByteArray)
{
imageLoder.loadBytes(file as ByteArray,context);
}
else
{
imageLoder.load(new URLRequest(file as String),context);
}
}
protected static function fileIsReady(event:Event):void
{
lastBitmap = imageLoder.content as Bitmap ;
onDone();
}
public static function isImage(targetFile:File):Boolean
{
var ext:String = targetFile.extension.toLowerCase();
return ext.indexOf('jpg')!=-1 || ext.indexOf('png')!=-1 || ext.indexOf('gif')!=-1;
}
/////////////////////////////////////////////////
/**You can activate the distriqt cameraUI by calling this function*/
public static function setUp(DISTRIQT_APPLICATION_KEY:String=null):void
{
DistriqtCameraUI.setUp();
}
}
}