Advertisement
loonerz

Custom Overlay for Camera

Nov 6th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var testOverlay = Ti.UI.createView({
  2.         bottom:0,
  3.         height:200,
  4.         width: platformWidth,
  5.         height : platformHeight,
  6.         left:0,
  7.         // backgroundColor : 'gray'
  8.     });
  9.    
  10.     var topbarView = Ti.UI.createView({
  11.         width: platformWidth,
  12.         height : resultHeight,
  13.         top : 0,
  14.         left : 0,
  15.         backgroundColor : '#363636',
  16.         opcatity : 0.6
  17.     });
  18.    
  19.     testOverlay.add(topbarView);
  20.    
  21.     var topbarTitle = Ti.UI.createLabel({
  22.         text:L("capture_a_moment_string"),
  23.         color:"#FFFFFF",
  24.         textAlign:"left",
  25.         width:Ti.UI.SIZE,
  26.         height:Ti.UI.SIZE,
  27.         left:"10dp",
  28.         font:{fontSize:"24dp", fontFamily: 'HelveticaNeue-UltraLight'} 
  29.     });
  30.    
  31.     topbarView.add(topbarTitle);
  32.    
  33.     var takeVideoIcon = Ti.UI.createImageView({
  34.         image:imagesPath +  "CameraOverlay_TakeVideo.png",
  35.         width:44 + dpi,
  36.         height:44 + dpi,
  37.         right:"10dp"
  38.     });
  39.    
  40.     topbarView.add(takeVideoIcon);
  41.    
  42.     var bottombarView = Ti.UI.createView({
  43.         width: platformWidth,
  44.         height : resultHeight,
  45.         bottom : 0,
  46.         left : 0,
  47.         backgroundColor : '#363636',
  48.         opcatity : 0.6
  49.     });
  50.    
  51.     testOverlay.add(bottombarView);
  52.    
  53.     var cameraButtonHolder = Ti.UI.createView({
  54.         width : Ti.UI.FILL,
  55.         height : 80 + dpi,
  56.         bottom : 0
  57.     });
  58.    
  59.     bottombarView.add(cameraButtonHolder);
  60.    
  61.     var cancelCameraButton = Ti.UI.createView({
  62.         width:80 + dpi,
  63.         height:80 + dpi,
  64.         left:0,
  65.         backgroundColor:"transparent"
  66.     });
  67.    
  68.     var cancelCameraIcon = Ti.UI.createImageView({
  69.         image:imagesPath +  "CameraOverlay_Close.png",
  70.         width:44 + dpi,
  71.         height:44 + dpi
  72.     });
  73.    
  74.     cancelCameraButton.add(cancelCameraIcon);
  75.    
  76.     var takePictureButton = Ti.UI.createView({
  77.         width : 80 + dpi,
  78.         height : 80 + dpi,
  79.         backgroundColor : 'transparent'
  80.     });
  81.    
  82.     var takePictureImage = Ti.UI.createImageView({
  83.         image:imagesPath +  "CameraOverlay_TakePicture.png",
  84.         width:44 + dpi,
  85.         height:44 + dpi
  86.     });
  87.    
  88.     takePictureButton.add(takePictureImage);
  89.    
  90.     var galleryButton = Ti.UI.createView({
  91.         width:80 + dpi,
  92.         height:80 + dpi,
  93.         right:0,
  94.         backgroundColor:"transparent"
  95.     });
  96.    
  97.     var galleryButtonImage = Ti.UI.createImageView({
  98.         image:imagesPath +  "CameraOverlay_Gallery.png",
  99.         width:44 + dpi,
  100.         height:44 + dpi
  101.     });
  102.    
  103.     galleryButton.add(galleryButtonImage);
  104.    
  105.     cameraButtonHolder.add(cancelCameraButton);
  106.     cameraButtonHolder.add(takePictureButton);
  107.     cameraButtonHolder.add(galleryButton);
  108.    
  109.     var cameraButtonSelectedColor = "#0e76bc";
  110.  
  111.  
  112.     cancelCameraButton.addEventListener("singletap", function(e){
  113.         Ti.Media.hideCamera();
  114.         cancelCameraButton.animate({
  115.             backgroundColor : "transparent",
  116.             duration : 150
  117.         }, function(e) {
  118.             parentWindow.closeView();
  119.         });
  120.  
  121.     });
  122.    
  123.  
  124.     takePictureButton.addEventListener('singletap', function(e){
  125.         Ti.Media.takePicture();
  126.     });
  127.    
  128.     var galleryOpened = false;
  129.     function openKineduPhotoGallery()
  130.     {
  131.         if(!galleryOpened)
  132.         {
  133.             galleryOpened = true;
  134.             Titanium.Media.openPhotoGallery({
  135.                 success : function(event) {
  136.                     Ti.API.info('Returning from selecting picture');
  137.                     var tmp;
  138.                     var mediaType;
  139.                     if (event.mediaType.indexOf('PHOTO') > -1 || event.mediaType.indexOf('image') > -1) {
  140.                         tmp = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, ('baby_temp.png'));
  141.                         mediaType = 'photos';
  142.                     }
  143.                     Ti.App.Properties.setString('mediaType', mediaType);
  144.  
  145.                     var imageFile = event.media;
  146.  
  147.                     var imageAsTaken = Ti.UI.createImageView({
  148.                         image : imageFile,
  149.                     });
  150.  
  151.                     imageAsTaken.transform(rotateMatrix);
  152.  
  153.                     var image = imageAsTaken.toBlob();
  154.                     var imageWidth = image.width;
  155.                     var imageHeight = image.height;
  156.                     var finalSize = imageWidth;
  157.                     var scaleX = 0;
  158.                     var scaleY = (imageHeight - imageWidth) / 2;
  159.                     if (imageHeight < imageWidth) {
  160.                         scaleX = (imageWidth - imageHeight) / 2;
  161.                         scaleY = 0;
  162.                         finalSize = imageHeight;
  163.                     }
  164.                     var croppedImage = image.imageAsCropped({
  165.                         x : scaleX,
  166.                         y : scaleY,
  167.                         width : finalSize,
  168.                         height : finalSize
  169.                     });
  170.  
  171.                     tmp.write(croppedImage);
  172.                    
  173.                     galleryOpened = false;
  174.                    
  175.                 },
  176.                 cancel : function(e) {
  177.                     Ti.API.info('Gallery cancelled');
  178.                     for(var x in e)
  179.                     {
  180.                         Ti.API.info('Property ' + x + ' value ' + e[x]);
  181.                     }
  182.                     galleryOpened = false;
  183.                 },
  184.                 error : function(error) {
  185.                     Ti.API.info('Gallery Error');
  186.                     for(var x in error)
  187.                     {
  188.                         Ti.API.info('Property ' + x + ' value ' + error[x]);
  189.                     }
  190.                     galleryOpened = false;
  191.                 },
  192.                 mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
  193.             });
  194.  
  195.         }
  196.     }
  197.    
  198.     galleryButton.addEventListener("singletap", function(e){
  199.         Ti.Media.hideCamera();
  200.         openKineduPhotoGallery();
  201.  
  202.     });
  203.     function showCamera() {
  204.         Ti.Media.showCamera({
  205.             success : function(event)
  206.             {
  207.                 Ti.API.info('Returning from taking picture');
  208.                 var tmp;
  209.                 var mediaType;
  210.                 if (event.mediaType.indexOf('PHOTO') > -1 || event.mediaType.indexOf('image') > -1)
  211.                 {
  212.                     tmp = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, ('baby_temp.png'));
  213.                     mediaType = 'photos';
  214.                 }
  215.                 else
  216.                 {
  217.                     tmp = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, ('baby_temp.mp4'));
  218.                     mediaType = 'videos'
  219.                 }
  220.                 Ti.App.Properties.setString('mediaType', mediaType);
  221.                
  222.                 if (mediaType == "photos")
  223.                 {
  224.                    
  225.                     var imageFile = event.media;
  226.                    
  227.                     var imageAsTaken = Ti.UI.createImageView({
  228.                         image : imageFile
  229.                     });
  230.                                        
  231.                     var image = imageAsTaken.toBlob();
  232.                     var imageWidth = image.width;
  233.                     var imageHeight = image.height;
  234.                     var finalSize = imageWidth;
  235.                     var scaleX = 0;
  236.                     var scaleY = (imageHeight - imageWidth) / 2;
  237.                     if(imageHeight < imageWidth)
  238.                     {
  239.                         scaleX = (imageWidth - imageHeight) / 2;
  240.                         scaleY = 0;
  241.                         finalSize = imageHeight;
  242.                     }
  243.                     var croppedImage = image.imageAsCropped({
  244.                         x : scaleX,
  245.                         y : scaleY,
  246.                         width : finalSize,
  247.                         height : finalSize
  248.                     });
  249.                    
  250.                     tmp.write(croppedImage);
  251.                 }
  252.                 Ti.Media.hideCamera();
  253.             },
  254.             cancel : function(e) {
  255.                 Ti.API.info('camera canceled');
  256.                 for(var x in e)
  257.                 {
  258.                     Ti.API.info('Property ' + x + ' value ' + e[x]);
  259.                 }
  260.                 parentWindow.closeView();
  261.             },
  262.             error : function(error) {
  263.                 Ti.API.info('camera error');
  264.                 for(var x in error)
  265.                 {
  266.                     Ti.API.info('Property ' + x + ' value ' + error[x]);
  267.                 }
  268.                 var message;
  269.                 if (error.code == Ti.Media.NO_CAMERA)
  270.                 {
  271.                     message = 'Device does not have camera capabilities';
  272.                 }
  273.                 else
  274.                 {
  275.                     message = 'Unexpected error: ' + error.code;
  276.                 }
  277.             },
  278.             allowEditing : false,
  279.             overlay:testOverlay,
  280.             mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO],
  281.             showControls: false,
  282.             videoMaximumDuration : 10000,
  283.             videoQuality : Titanium.Media.QUALITY_MEDIUM,
  284.             saveToPhotoGallery : false,
  285.             autohide : true
  286.         });
  287.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement