Rotate and save image in Titanium? - android

I am currently working on an android app that allow user to rotate the image then save it. I am able to save the image but when I check it, it doesn't rotate. Would appreciate if someone can point me to a right direction.
Here is my code:
_rotateLeft.addEventListener('click', function(e){
_rotationDegree = _rotationDegree - 90;
var t = Ti.UI.create2DMatrix();
//t.rotate(90);
//imageContainer.transform = t;
imageContainer.animate({
transform: t.rotate(_rotationDegree)
});
});
_saveButton.addEventListener('click', function(){
var imgSave = imageView.toImage();
var moment = require('alloy/moment');
var directory = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, 'images/test');
!directory.exists() && directory.createDirectory();
var fileName = String.format('rotate_%s.jpg', moment().format('YYYY-MM-DD-HH-mm'));
var file = Ti.Filesystem.getFile(directory.resolve(), fileName);
var fileNativePath = file.nativePath;
// Write media to file
//file.write(newBlob);
file.write(imgSave);
Ti.API.info('New Save File : ' + JSON.stringify(file));
imageView.setImage(file);
});
Here is my view:
<View id="imageContainer">
<!--<View id="imageUploadedView"></View>-->
<ImageView id="imageView"></ImageView>
</View>

There are 2 things I found odd in your code:
1 - You are rotating imageContainer and then you are capturing imageView.toImage(); which I think is the cause of issue because:
Rotating container view rotates the child elements also, and capturing a child.toImage(); will return you the UI of that child irrespective of the rotation (though I am not 100% sure because I would prefer below step to do this task).
So to make to properly work, rotate imageView and use imageContainer.toImage(); so that child view is rotated and parent view can save its rotated state.
2 - Inside _saveButton click event, use this line:
imageView.setImage(file.read()); because you were not reading the blob data of the file in your code.

You need to declare the transform first then apply the transform to the view before saving.
_rotateLeft.addEventListener('click', function(e){
var rotateTransform = Ti.UI.create2DMatrix({
rotate: 45
});
imageContainer.transform = rotateTransform;
}
Reference: http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.2DMatrix

Related

fl_chart touch response issue in LineChart

I have displayed a Line chart using fl_chart ^0.10.1 (Tested on Flutter ver 52.2.1 & 53.0.1).
I am updating the FlSpots from a csv file.
I am trying to get the index or Spot object if I touch somewhere in the plot.
But I am getting an empty list from the touchResponse if I touch (both tap or long-press) somewhere.
But I am getting the value in the plot itself. I am assuming it's there because of the built-in touch handler.
The code I am trying it:
lineTouchData: LineTouchData(
touchCallback: (LineTouchResponse touchResponse) {
if (touchResponse.touchInput is FlPanEnd ||
touchResponse.touchInput is FlLongPressEnd) {
var spots = touchResponse.lineBarSpots;
setState(() {
var lengthList = spots.length;
print(
'touched spot---> ${spots.toString()},,,len--->$lengthList');
});
} else {
print('wait');
}
}),
My end goal is to use and pass the touched index to open a new screen/widget.
touchCallback: (LineTouchResponse? lineTouch) {
final value = lineTouch.lineBarSpots![0].x;
// set state...
});
It's in the demo by fl_chart in github.
https://github.com/imaNNeoFighT/fl_chart/blob/master/example/lib/line_chart/samples/line_chart_sample3.dart

Titanium - Send picture to server : file size bug

I'd like to send pictures, from my app (iOS and Android) to my server. My code works with small pictures, but if the size is too big, when I send the data, nothing happens and the application slows down.
Could you explain me the problems in my code and how to resolve it ? Thanks a lot :)
Here is my code :
var attached_media = [];
var file_btn = Ti.UI.createButton({ title: L('select') });
file_btn.addEventListener('click',function(e){
Titanium.Media.showCamera({
success:function(e) {
if(e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
attached_media.push(Ti.Utils.base64encode(e.media).text);
}
},
saveToPhotoGallery:true,
allowEditing: false,
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO]
});
});
var send_button = Titanium.UI.createButton({
title: 'Send',
});
send_button.addEventListener('click',function(e){
var req = ......
req.send({ 'medias':JSON.stringify(attached_media), 'user_id':Ti.App.Properties.getInt('user_id')});
});
I removed the unnecessary code, because it was too long ! :)
What I was able to understand from the provided info is that you are having problems while uploading large size pics, like the one from camera which turns out to be more than 2-3MB.
The only solution at present I can suggest you is to compress the image using this iOS-Android module Ti-ImageFactory before saving or sending it to server.
I recommend to compress the image right after you captured it in Camera's success callback like this:
file_btn.addEventListener('click',function(e){
Titanium.Media.showCamera({
success:function(e) {
if(e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
Ti.API.info("Initial pic bytes = " + e.media.length);
// if bytes length of pic is larger than 3MB or 3145728 bytes, set compression to 0.5,
// else keep it to default which is 0.7
var imf = require('ti.imagefactory');
var compressedPic = (e.media.length > 3145728) ? imf.compress(0.5) : imf.compress();
attached_media.push(Ti.Utils.base64encode(compressedPic).text);
Ti.API.info("Compressed pic bytes = " + compressedPic.length);
compressedPic = null;
}
},
saveToPhotoGallery:true,
allowEditing: false,
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO]
});
});
Added Code - If captured picture size is more than 3MB, then compress it by 0.5 level, else compress it using default level 0.7. Also checking the initial pic size & compressed pic size to match better results as per app's requirements for faster uploading.
You can also pass a compression level in compress() method. See docs for more.

Add sound switch off/on button corona sdk

I need help with adding sound off/on button to my game. In global variable lua file, I have the following:
local sounds = {}
sounds["select"] = audio.loadSound("sounds/select.mp3")
sounds["score"] = audio.loadSound("sounds/score.mp3")
G.playSound = function(name)
if sounds[name] ~= nil then
audio.play(sounds[name])
end
end
In games.lua file, I call the function as:
utils.playSound("score")
I have a soundon.png and soundoff.png files both in a sprite sheet (not sure if that is a good idea), all I am trying to implement is when you click the sound button, all sounds stops and it displays the soundoff image, vice versa.
Thanks
I personally wouldn't use a sprite sheet. Just load both images and toggle their "isVisible" field. Then toggle a variable which would stop your sounds. Try something like this.
myGlobalSoundToggle = true
local image = display.newImage("soundon.png")
local image2 = display.newImage("soundoff.png")
image2.isVisible = false
local function onTap( self, event )
image.isVisible = ~image.isVisible
image2.isVisible = ~image2.isVisible
myGlobalSoundToggle = image.isVisible
return true
end
image:addEventListener( "tap", onTap )
Now that we have our buttons working, we need to toggle the sound on and off.
local sounds = {}
sounds["select"] = audio.loadSound("sounds/select.mp3")
sounds["score"] = audio.loadSound("sounds/score.mp3")
G.playSound = function(name)
if (sounds[name] ~= nil) and (myGlobalSoundToggle) then
audio.play(sounds[name])
end
end

How to stop shifting of content when screen is rotated form portrait to landscape view in mobile devices?

The content is getting displaced in the viewport when screen is rotated from portrait view to landscape view and viceversa.
It only happens when I see the middle or end part of the html document.
you can see the problem hare but try to open it in mobile browser or try using combination ctrl-shift-m on mozilla
I tried different viewport settings but nothing seems to be working.
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;,maximum-scale=1, user-scalable=no" name="viewport">
And one more thing if we make the height of images fixed then it start working in the right manner .But due to its responsive nature I think its not good to give images a fixed height.
Pleas suggest your solution.
Thanks every one for taking interest in my question.
I was thinking it was an css issue or html bug but I fixed it with small jQuery code.
Also see the working example
jQuery(function(){
jQuery.miniMenu = new Object();
jQuery.miniMenu.i = new Object();
jQuery(document).on("scroll", function(){
var scroll_position = jQuery(window).scrollTop();
var doc_height = jQuery(document).height();
var w_height = jQuery(window).height();
var b_h = doc_height-w_height;
var scroll = (scroll_position/b_h)*100;
jQuery.miniMenu.i.globalVar = scroll;
console.log("scroll position "+scroll);
});
jQuery(window).click(function(){
console.log("new scroll "+jQuery.miniMenu.i.globalVar);
var scroll_position = jQuery(window).scrollTop();
var doc_height = jQuery(document).height();
var w_height = jQuery(window).height();
var b_h = doc_height-w_height;
var scroll = (scroll_position/b_h)*100;
console.log("scroll position "+scroll);
});
jQuery(window).on('orientationchange', orientationChangeHandler);
console.log("orentation change");
function orientationChangeHandler(e) {
setTimeout(function() {
var doc_height = jQuery(document).height();
var scroll = jQuery.miniMenu.i.globalVar;
var w_height = jQuery(window).height();
var b_h = doc_height-w_height;
var scroll_position = (scroll*b_h)/100;
scroll_position = Math.round(scroll_position);
jQuery('html, body').animate( {scrollTop: scroll_position}, "5", function(){
console.log("animation complete");
});
console.log(scroll_position);
}, 20);
}
});

How to show the last captured image from the camera without code in Image view

I am new to the Titanium Android Mobile application development.
My problem is how do i load the last captured image of camera in an image view.
I have a bitton called as click in one window and I have to display that image in second window.
How will I achieve this , remebr no code is needed only using the Titanium android.
Thanks
Try this code...
Ti.Media.showCamera({
success : function(event) {
if (event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
// Here you can do whatever you want with the image captured from the camera
var imgView = Ti.UI.CreateImageView({
image: event.media,
width: Ti.UI.SIZE, height: Ti.UI.SIZE
});
Ti.UI.currentWindow.add(imgView); // It will be added to the centre of the window if you didn't specify top or left or ...
} else {
alert("got the wrong type back =" + event.mediaType);
}
},
cancel : function() {
alert("You have cancelled !");
},
error : function(error) {
alert("error");
},
saveToPhotoGallery : true,
allowEditing : true,
mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
});
You will receive the captured image data via a method of the Callback interface.
Callback interface is used to supply image data from a photo capture.
Camera.PictureCallback
onPictureTaken(byte[] data, Camera camera)
{
......
Your code
......
}
You can do whatever you want to do with image data here. Means you can set the image to imageview here. You will receive data in bytes. Simply convert it to drawable/bitmap and set that drawable/bitmap to the imageview. Thats it!
For converting bytes to bitmap you can use this link :
How to convert byte array to Bitmap
Thanks for all the response ...
I have solved this my own.
var image = event.media;
then capture the native path for the image using nativePath
image.nativePath
and then store this path to the application property and reuse this property using Application getString function.

Categories

Resources