Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Instagram like photo cropping?

$
0
0

I have been working on getting a simple square 'viewport' to overlay an image and then save the new square image. I have gotten all the drag and drop/border detection done but cannot figure out how to save the image. I found this (http://developer.appcelerator.com/question/72431/crop-imageview) but it wont work for a few different reasons. Does anyone know of any code or modules that would allow me to let the user crop an image to a square and then save that image?

FYI: This has only been tested on Android so far.

Here is my code in case anyone is interested. Looking for any suggestion..

var baseWidth = 0, baseHeight = 0;
var maxWidth = 0;
var imageHeight = 0;
var maxTop = 0, maxBottom = 0;
var halfWidth = 0, halfHeight = 0;
var oldX = 0, oldY = 0;
 
//get lesser of 2
if (Ti.Platform.displayCaps.platformWidth <= Ti.Platform.displayCaps.platformHeight) {
    maxWidth = baseWidth = baseHeight = Ti.Platform.displayCaps.platformWidth;
} else {
    maxWidth = baseWidth = baseHeight = Ti.Platform.displayCaps.platformHeight;
}
 
var win = Ti.UI.createWindow({
    backgroundColor : '#000',
    fullscreen : false,
    hideNavBar : true,
    exitOnClose : true,
    name : 'win'
});
 
var imageView = Ti.UI.createImageView({
    image : 'http://msgboard.snopes.com/photos/odd/graphics/watermelon3.jpg',
    width : '100%',
    zIndex : 0,
    touchEnabled : false
});
win.add(imageView);
 
var cropView = Ti.UI.createView({
    height : baseHeight,
    width : baseWidth,
    borderColor : 'black',
    borderWidth : 8,
    backgroundColor : 'transparent',
    zIndex : 1,
    touchEnabled : false
});
win.add(cropView);
 
win.addEventListener('pinch', function(e) {
    var newWidth = baseWidth * e.scale;
    var newHeight = baseHeight * e.scale;
 
    //dont allow to scale larger than smallest distance
    if (newHeight <= maxWidth || newWidth <= maxWidth) {
        cropView.height = baseHeight * e.scale;
        cropView.width = baseWidth * e.scale;
        halfWidth = cropView.width / 2;
        halfHeight = cropView.height / 2;
    }
 
});
win.addEventListener('touchstart', function(e) {
    if (imageHeight == 0) {
        //set first time properties
        imageHeight = imageView.toImage().height;
        maxTop = ((Ti.Platform.displayCaps.platformHeight / 2) - (imageHeight / 2)) / 2;
        maxBottom = imageHeight + maxTop;
    }
    baseHeight = cropView.height;
    baseWidth = cropView.width;
});
 
win.addEventListener('touchmove', function(e) {
    if (e.source.name == 'win') {
        //if all in bounds, move both ways
        if (e.x - halfWidth >= 0 && e.x + halfWidth <= maxWidth && e.y - halfHeight >= maxTop && e.y + halfHeight <= maxBottom) {
            cropView.animate({
                center : {
                    x : e.x,
                    y : e.y
                },
                duration : 0
            });
            oldX = e.x;
            oldY = e.y;
        } else if (e.x - halfWidth >= 0 && e.x + halfWidth <= maxWidth) {
            //move left and right
            cropView.animate({
                center : {
                    x : e.x,
                    y : oldY
                },
                duration : 0
            });
            oldX = e.x;
        } else if (e.y - halfHeight >= maxTop && e.y + halfHeight <= maxBottom) {
            //move up and down
            cropView.animate({
                center : {
                    x : oldX,
                    y : e.y
                },
                duration : 0
            });
            oldY = e.y;
        }
 
    }
});
 
var btnTake = Ti.UI.createButton({
    title : 'take picture',
    bottom : 0,
    left : 0
});
btnTake.addEventListener('click', function(e) {
    var newImageview = Ti.UI.createImageView({
        width : '100%',
        image : cropView.toImage(),
        zIndex : 20
    })
    win.add(newImageview);
});
 
win.add(btnTake);
win.open();

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>