• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

reducing camera roll size

Participant ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

what do i need to put into my code here that will reduce the size of the image I load onto my stage from the camera roll? I change the physical dimensions but its just obviously still loading the larger imagine into the bitmapdata. Im finding if the user loads a huge file it totally crashes the app. I want to reduce it to 1024px on the width

function checkPermission (e:Event=null):void

{

if (CameraRoll.permissionStatus != PermissionStatus.GRANTED) {

  cameraRoll2.addEventListener(PermissionEvent.PERMISSION_STATUS, function (e: PermissionEvent): void {

  if (e.status == PermissionStatus.GRANTED) {

  launchCamera()

  } else {

  // permission denied

  }

  });

  try {

  cameraRoll2.requestPermission();

  } catch (e: Error) {

  // another request is in progress

  }

} else {

  launchCamera()

}

}

function launchCamera():void {

if (CameraRoll.supportsBrowseForImage) {

_roll = new CameraRoll();

_roll.browseForImage();

_roll.addEventListener(MediaEvent.SELECT, onSelect);

_roll.addEventListener(Event.CANCEL, onCancel);

} else {

}

}

function onSelect(event:MediaEvent):void {

  activityDialog = Dialog.service.create(

  new ActivityBuilder()

  .setTheme( new DialogTheme( DialogTheme.DEVICE_DEFAULT_DARK ))

  .build()

  );

activityDialog.show();

_roll.removeEventListener(MediaEvent.SELECT, onSelect);

_roll.removeEventListener(Event.CANCEL, onCancel);

var imagePromise:MediaPromise = event.data;

_load = new Loader();

if (imagePromise.isAsync) {

_load.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );

_load.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, imageLoadFailed );

_load.loadFilePromise(imagePromise);

} else {

_load.loadFilePromise( imagePromise );

sourcePhoto="local"

}

}

function imageLoaded( evt:Event ):void {

  if(myBitmapData==null)

  {

  }

  else

  {

  myBitmapData.dispose()

  myBitmapData = null

  target_mc.removeChildAt(0)

  }

  evt.target.content.width = 900;

  evt.target.content.scaleY = evt.target.content.scaleX;

  if(evt.target.content.height>700)

  {

  evt.target.content.height = 700;

  evt.target.content.scaleX = evt.target.content.scaleY;

  }

  if(evt.target.content.height<700)

  {

  evt.target.content.y = (700-evt.target.content.height)/2

  }

  if(evt.target.content.width<900)

  {

  evt.target.content.x = (900-evt.target.content.width)/2

  }

target_mc.addChild(evt.target.content); //add the content

myBitmapData = Bitmap(LoaderInfo(evt.target).content).bitmapData;

trace(myBitmapData.width);

trace(myBitmapData.height);

}

function imageLoadFailed( event:Event ):void {

}

function onCancel(event:Event):void {

_roll.removeEventListener(MediaEvent.SELECT, onSelect);

_roll.removeEventListener(Event.CANCEL, onCancel);

}

TOPICS
Development

Views

357

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 13, 2018 Feb 13, 2018

Copy link to clipboard

Copied

You can't change the size of the picture prior to loading it, that's the basic answer.

As far as crashing the app, I don't think loading a picture would do that unless its dimension are higher than the max limit of 4096*4096 and still I don't think this will give you a crash.

My advice: investigate what is really causing the crash, what are the required conditions? What size pictures are NOT causing a crash and what size picture ARE causing a crash then > put some traces here and there and find out what line is causing the crash > then you will have all the information you need to assess the problem and find solutions for it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 13, 2018 Feb 13, 2018

Copy link to clipboard

Copied

Hi

its not crashing on loading, sorry i wasn't clear. It is loading the picture but when i then try and share the bitmapdata it will either crash or do so incredibly slowly, once the picture is loaded the app will run slow. The picture is 15mb in size - over 5000 pixels.

I can redraw it can't i into the bitmap data so the image thats being shared isn't 5000+ pixels but 1024? Either with a matrix or jpeg encoder or something?

i'm not sure, i just had a feeling i could

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 13, 2018 Feb 13, 2018

Copy link to clipboard

Copied

Once it's loaded you should be able to redraw it into a smaller bitmapdata (mybitmapdata.draw(ect)), the process of drawing in bitmapdata is usually slow on mobile but a good work around is to develop an ane for that purpose.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 13, 2018 Feb 13, 2018

Copy link to clipboard

Copied

Here's is an example on how to resize a bitmap (it's coming from a static class). Pass your bitmapdata you want to resize, pass a maxwidth and maxheight and you'll get back a resized bitmapdata. If you pass false as last parameter then the maxheight will be ignored and the resizing will be done by ratio width/height.

public static function resize(target:BitmapData, maxwidth:Number, maxHeight:Number, forceDimension:Boolean = false):BitmapData

  {

  if(!forceDimension)

  {

  var ratio:Number = target.width / target.height;

  var width:Number = maxwidth;

  var height:Number = width / ratio;

  if (height > maxHeight)

  {

  height = maxHeight;

  width = height * ratio;

  }

  }

  else

  {

  width = maxwidth;

  height = maxHeight;

  }

  var copy:BitmapData = BitmapCache.getBitmapData(width, height, true, 0x00FF0000);

  var matrix:Matrix = new Matrix();

  matrix.scale(width / target.width, height / target.height);

  copy.drawWithQuality(BitmapCache.getBitmap(target) as IBitmapDrawable, matrix, null, null, null, true, StageQuality.BEST);

  return copy;

  }

Replace "BitmapCache.getBitmapData" by "new BitmapData" and "BitmapCache.getBitmap(target) as IBitmapDrawable" by "target"

Once you have resized the bitmapdata don't forget to unload/dispose of the original picture

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 13, 2018 Feb 13, 2018

Copy link to clipboard

Copied

thats the part i don't know how to do. The redraw part. Do you have any example I could use?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 13, 2018 Feb 13, 2018

Copy link to clipboard

Copied

LATEST

great thanks, ill give that a go.

thanks for your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines