TItanium - How to take pictures in the right orientation using an overlay? - ios

I'm working on Titanium SDK 3.1.1 and deploying for iOS. What I'm trying to do is to open the camera with an overlay, take a picture, crop the picture and save it. But I'm having trouble to take a picture with an overlay.
If I take a picture in portrait, the image returned is taken in landscape. When taking it in landscape, the result will be a portrait image. No idea why is this happening.
My overlay is really simple, it has three views that act as buttons, one of them closes the camera, another takes the picture and the other opens the gallery (which throws an exception, but that's a problem for a different question):
var testOverlay = Ti.UI.createView({
bottom:0,
height:200,
width: platformWidth,
height : platformHeight,
left:0
});
//just a black view on top to limit upper area
var topbarView = Ti.UI.createView({
width: platformWidth,
height : resultHeight,
top : 0,
left : 0,
backgroundColor : '#363636',
opcatity : 0.6
});
testOverlay.add(topbarView);
// just a black view on bottom to limit bottom area and display custom controls
var bottombarView = Ti.UI.createView({
width: platformWidth,
height : resultHeight,
bottom : 0,
left : 0,
backgroundColor : '#363636',
opcatity : 0.6
});
testOverlay.add(bottombarView);
// container view for custom controls
var cameraButtonHolder = Ti.UI.createView({
width : Ti.UI.FILL,
height : 80 + dpi,
bottom : 0
});
bottombarView.add(cameraButtonHolder);
//a view to close camera
var cancelCameraButton = Ti.UI.createView({
width:80 + dpi,
height:80 + dpi,
left:0,
backgroundColor:"pink"
});
// a view to take picture
var takePictureButton = Ti.UI.createView({
width : 80 + dpi,
height : 80 + dpi,
backgroundColor : 'blue'
});
//a view to open gallery
var galleryButton = Ti.UI.createView({
width:80 + dpi,
height:80 + dpi,
right:0,
backgroundColor:"green"
});
cameraButtonHolder.add(cancelCameraButton);
cameraButtonHolder.add(takePictureButton);
cameraButtonHolder.add(galleryButton);
// event handler to close camera
cancelCameraButton.addEventListener('click', function(e){
Ti.Media.hideCamera();
parentWindow.closeView();
});
//event handler to take picture
takePictureButton.addEventListener('click', function(e){
Ti.Media.takePicture();
});
// event handler to open gallery
galleryButton.addEventListener("click", function(e){
Titanium.Media.openPhotoGallery({
success : function(event) {
Ti.API.info('Returning from gallery');
var filename = 'baby_temp.png';
var tmp = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, (filename));
tmp.write(event.media);
var blob = tmp.read();
Ti.App.fireEvent(callback, {
media_type : 'photos'
});
},
cancel : function() {
Ti.API.info('Cancelled');
},
error : function(error) {
// if error, show message
var message = 'Unexpected error: ' + JSON.stringify(error);
Ti.UI.createAlertDialog({
title : 'Camera',
message : message
}).show();
},
mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
});
});
My function that opens the camera and handles the photo looks like this:
function showCamera() {
Ti.Media.showCamera({
success : function(event)
{
var tmp;
var mediaType;
if (event.mediaType.indexOf('PHOTO') > -1 || event.mediaType.indexOf('image') > -1)
{
tmp = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, ('baby_temp.png'));
mediaType = 'photos';
}
else
{
tmp = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, ('baby_temp.mp4'));
mediaType = 'videos'
}
Ti.App.Properties.setString('mediaType', mediaType);
if (mediaType == "photos")
{
//If it's a picture then crop it and save it
var image = event.media;
var imageWidth = image.width;
var imageHeight = image.height;
var scaleX = 0;
var scaleY = (imageHeight - imageWidth) / 2;
if(imageHeight > imageWidth)
{
scaleX = (imageWidth - imageHeight) / 2;
scaleY = 0;
}
var croppedImage = image.imageAsCropped({
x : scaleX,
y : scaleY,
width : imageWidth,
height : imageWidth
});
tmp.write(croppedImage);
}
else
{
// video
tmp.write(event.media);
}
Ti.App.fireEvent('changeCreateMoment', {
media_type : mediaType
});
Ti.Media.hideCamera();
},
cancel : function() {
// hide camera and close the window
Ti.Media.hideCamera();
parentWindow.closeView();
},
error : function(error) {
// if error, show message, hide camera and close window
var message;
if (error.code == Ti.Media.NO_CAMERA)
{
message = 'Device does not have camera capabilities';
}
else
{
message = 'Unexpected error: ' + error.code;
}
Ti.UI.createAlertDialog({
title : 'moments',
message : message
}).show();
Ti.Media.hideCamera();
parentWindow.closeView();
},
overlay:testOverlay,
mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO, Ti.Media.MEDIA_TYPE_VIDEO],
videoMaximumDuration : 10000,
videoQuality : Titanium.Media.QUALITY_MEDIUM,
saveToPhotoGallery : false,
showControls : false,
autohide : false
});
}
I have no idea why the photo is taken in landscape instead of portrait and vice versa. Am I missing a property in my showCamera function call? Why am I getting a photo like this?

Related

PDFJS rendering pdf with different pages orientations (text layer)

I use PDF.JS to annotate PDFs on my website.
It works fine, but I have a problem with the "Text Layer" when the PDF contains pages in different orientations (portrait and landscape).
Here is a screenshot that shows the situation:
screenshot
Here is my code:
function renderPage(pageNumber, canvas, annotationLayer, textLayer) {
__PDF.getPage(pageNumber).then(function(page) {
viewport = page.getViewport({
scale: __SCALE
});
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: canvas.getContext('2d'),
viewport: viewport
};
// Render the page contents in the canvas
page.render(renderContext).promise.then(function() {
return page.getTextContent();
}).then(function(textContent) {
console.log(pageNumber, textContent);
var textLayer = new pdfjsLib.renderTextLayer({
container: $('.page-container-' + pageNumber + ' .text-layer').get(0),
pageIndex: page.pageIndex,
viewport: viewport,
textContent: textContent,
enhanceTextSelection: true,
});
// Clear HTML for text layer and show
$('.page-container-' + pageNumber + ' .text-layer').html('').show();
// Assign the CSS created to the text-layer element
$('.page-container-' + pageNumber + ' .text-layer').css({
height: canvas.height + 'px',
width: canvas.width + 'px'
});
})
});
}
PDF with only landscape or only portrait pages work fine...
Thanks for your help!

yAxis resizer to change svgrenderer position also highcharts

I have a chart with some indicators below it. Each indicator area consists
of a svg renderer button. so when I use resize property to drag and resize
the panes, the series resized perfectly but the button remains in its same
position, Can we move the button with the resizer?
Here I created a sample link to regenerate
https://jsfiddle.net/q0ybpnvx/2/
Any help will be appreciated. I am having great trouble. Thank you
You can add and position the custom button in render event:
chart: {
events: {
render: function() {
var chart = this;
if (chart.customBtn) {
chart.customBtn.attr({
y: chart.yAxis[1].top,
});
} else {
chart.customBtn = chart.renderer.button(
'sometext',
5,
chart.yAxis[1].top,
function() {
console.log('some task')
}).add()
}
}
}
},
Live demo: https://jsfiddle.net/BlackLabel/b7vq4ecy/
API Reference:
https://api.highcharts.com/highcharts/chart.events.render
https://api.highcharts.com/class-reference/Highcharts.SVGElement#attr
I was able to do something by manually changing the x/y attributes of my svgRenderer label, the same should apply to buttons.
I'm in angular and have a listener for screen resizing:
Also Note that you can change the entire SVGRenderer label with the attr.text property.
this.chart = Highcharts.chart(.....);
// I used a helper method to create the label
this.chart.myLabel = this.labelCreationHelperMethod(this.chart, data);
this.windowEventService.resize$.subscribe(dimensions => {
if(dimensions.x < 500) { //this would be your charts resize breakpoint
// here I was using a specific chart series property to tell where to put my x coordinate,
// you can traverse through the chart object to find a similar number,
// or just use a hardcoded number
this.chart.myLabel.attr({ y: 15, x: this.chart.series[0].points[0].plotX + 20 });
} else {
this.chart.myLabel.attr({ y: 100, x: this.chart.series[0].points[0].plotX + 50 });
}
}
//Returns the label object that we keep a reference to in the chart object.
labelCreationHelperMethod() {
const y = screen.width > 500 ? 100 : 15; 
const x = screen.width > 500 ? this.chart.series[0].points[0].plotX + 50 :
this.chart.series[0].points[0].plotX + 20
// your label
const label = `<div style="color: blue"...> My Label Stuff</div>`
return chart.renderer.label(label, x, y, 'callout', offset + chart.plotLeft, chart.plotTop + 80, true)
.attr({
fill: '#e8e8e8',
padding: 15,
r: 5,
zIndex: 6
})
.add();
}

EventListener issues for images in ScrollableView for IOS Appcelerator

I have added some images in two different views & placed them inside a ScrollableView. On click of images, EventListener is not getting executed. This code works perfectly for Android(appcelerator) but for IOS it's not working.
Scroll & other Events are working fine. The problem seems with items placed in ScrollableView.
var dataView = Ti.UI.createView({
layout : 'horizontal',
top : '0'
});
var textHolderView = Ti.UI.createView({
layout : 'vertical',
width : '50%'
});
var iconView = Ti.UI.createView({
layout : 'horizontal',
width : '24.5%'
});
var header = Ti.UI.createLabel({
text : "Some Text",
font : {
fontSize : '12dp',
fontFamily : 'OpenSans-Semibold',
fontWeight : 'normal'
},
height : '38dp',
left : '15%',
color : '#000',
width : '85%',
touchEnabled : false
});
var image1 = Ti.UI.createImageView({
image : "/images/individual.png",
height : '18dp',
left : '5dp',
width : '13%',
top : '25%'
});
var slideOptionsView = Ti.UI.createView({
backgroundColor : "#1268b3",
layout : "horizontal"
});
var img_activity = Ti.UI.createImageView({
image : "/images/activity_temp.png",
height : "30dp",
width : "70dp",
left : "10%",
top : "15dp",
touchEnabled : true
});
textHolderView.add(header);
iconView.add(image1);
dataView.add(textHolderView);
dataView.add(iconView);
slideOptionsView.add(img_activity);
var scroll = Ti.UI.createScrollableView({
views : [dataView, slideOptionsView],
showPagingControl : false,
});
// Event Listeners on click for above images
img_activity.addEventListener('click', function(e) {
alert("img_activity");
});
image1.addEventListener('click', function(e) {
alert("image1");
});
$.someView.add(scroll);
$.index.open();
Thanks.

changing image view into a blob does not always work .toImage();

The following code is working for the most part, however sometimes after being changed into a blob, the image view does not display the image.
//store in temp image view, convert to blob
imageViewTemp.image = "imagename.jpg"
blob = imageViewTemp.toBlob();
albumTitle = data[x].name + ' (' + numberPhotos + ')';
var row = Titanium.UI.createTableViewRow({
titleAlb : data[x].name,
width : '100%',
height : 'auto'
});
var image = Titanium.UI.createImageView({
top : 0,
left : 0,
width : '75',
height : '75'
});
var title = Titanium.UI.createLabel({
text : albumTitle,
top : 0,
left : 110,
width : 'auto',
height : 'auto'
});
var width = blob.width;
var height = blob.height;
//crop so it fits in image view
if (width > height) {
image.image = ImageFactory.imageAsCropped(blob, {
width : height,
height : height,
x : 60,
y : 0
});
} else {
image.image = ImageFactory.imageAsCropped(blob, {
width : (width - 1),
height : (width - 1),
x : 60,
y : 0
});
}
row.add(image);
row.add(title);
rows.push(row);
}
In order to change the dimension of the image, I am using a module called image factory. Before I can change it, I have to store the image inside a tempory image view which I then convert into a blob:
blob = imageViewTemp.toBlob();
The problem is after the screen is rendered sometimes this will not work:
image.image = ImageFactory.imageAsCropped(blob, {
width : height,
height : height,
x : 60,
y : 0
});
Othertimes it will.
I have read online that the problem might be linked to the post layout cycle, but I am not sure, or how to proceed
http://developer.appcelerator.com/question/174329/what-are-the-difference-between-toblob--toimage
All help appreciated.
Solved the problem.
You have to download the image first using a REST API method (GET) before placing it into the temporary image view, otherwise the temp image view will be rendered before the file has been fully downloaded (.toImage is an async call back method), making way for a useless blob and no image.
The only problem with this method, is that you are reliant on your REST API calls to not fail.

Label View eventlistener not working in Table View

I am creating Array of LabelView in my application during the run time and adding it to a TableView. But click EventListener not working for LabelView, Here is my sample code
var picRow = Titanium.UI.createTableViewRow();
var photoContainer = Ti.UI.createView({
top:0,
width:300,
left:15,
right:5,
height:200
})
picRow.add(photoContainer);
var shareTable = Ti.UI.createTableView({
data:[picRow],
top:10,
left:0,
right:0,
height:250,
separatorColor: '#ccc',
backgroundColor:'transparent'
});
win.add(shareTable);
var pushleft = 5;
var pushtop = 5;
var images = [];
for (var i = 0; i < imageArray.length; i++){
images[i] = Ti.UI.createLabel({
backgroundImage: imageArray[i].image, // path to image at applicationDataDirectory
width: 70 ,
height: 70,
"tickOption":false,
"index":i,
"picId":imageArray[i].picId,
left: pushleft + 25, // logic for positioning
top: pushtop + 5
});
pushleft = pushleft + 80;
pushtop = pushtop + 50;
photoContainer.add(images[i]);
images[i].addEventListener('click', function(e) {
alert(e);
});
}
Here I am not getting any alert while clicking on the label view. Also I am not getting any error. Anyone help me to solve this issue. Thanks in Advance. I am using Titanium 1.7.5 and ios
it is a best practice to put the eventlistener on the whole row and then look at the click event object to determine which object received the click.
so for example you would look for the picId of the object, if it existed, you would know that a label was clicked

Resources