I am currently implementing some application using cocos2d that requires capturing image from camera and applying some effects. In order to apply these effects images should be added to CCNode. So my question is: how can I capture image from camera and save it to CCSprite or somehow add it to CCLayer?
I would much appreciate your help!
yes you can do that and it is quite easy!
Once you use the camera to get an image you will have an UIImage object. Then all you have to do is this:
CGImageRef imageref = [image CGImage]; //image is the UIImage retrieved from the camera or photo library.
CCSprite * mySprite =[CCSprite spriteWithCGImage:imageref key:nil];
You can then treat mySprite as any regular sprite.
Related
I'm trying to build a sprite from an image. Using Image and Texture2D class and then later creating a sprite from the texture2D.
The image I am loading is 512x512 and I expected both versions of the createWithTexture to behave the same but they don't. Here the code:
Image* image = new Image();
image->initWithImageFile(fileName);
Texture2D* texture = new Texture2D();
texture->initWithImage(image);
//If used this way everything works as expected
Sprite* spr= Sprite::createWithTexture(texture);
//If used with a Rect weird result occurrs.
Sprite* spr= Sprite::createWithTexture(texture,Rect(0,0,512,512));
spr->setAnchorPoint(Vec2(0,0));
spr->setPosition(Vec2(0,0));
spr->setScale(1.0f,1.0f);
this->addChild(spr);
Here the result of the first one using a Rect:
And here the Second version without a Rect:
Do anybody know what is happening? I need to use the method that uses the rect because I will be creating a bunch of sprites from this image in the future.
Edit1: After debugging both versions of the sprite. I have noticed that the one created without the Rect shows a rect of 0,0,240,240. Instead of 0,0,512,512 as I provided. Why 240?
Thanks in advance.
I managed to figure out what was happening. Cocos2D-x uses director->setContentScaleFactor and glview->setDesignResolutionSize as a way to make things easier for multi resolution/device games. When you build the Rect to get a part (or full) texture you must have into account the CC_CONTENT_SCALE_FACTOR() macro, in order to get correct target coordinates.
This can be checked at this link: http://www.cocos2d-x.org/wiki/Multi_resolution_support
Cheers.
If your vecSize is bigger than the image's size, the image will be out of shape.
So if you don't know the image's real size, don't set it.
I have a problem while scaling and transforming QGraphicsPixmapItem. I have created graphics editor in which I am using QGraphicsItem of initial size 100,100 and after that I am resizing and rotating it by setTransform() method.
After that I am storing this transform and using these for my another application called player in which I am using those transform and showing images and videos on it, I am using QgraphicsPixmapItem to show images now if I set scaled and setTransform methods to image its clarity gets spoiled.
Is it possible to keep clarity as it is even if I scaled and transform it.The image I am using is of 2560x1440 pixels.
Any help will be appreciated.
The code is :
QGraphicsPixmapItem *pixitem = new QGraphicsPixmapItem;
pixitem->setFlags(QGraphicsItem::ItemIsMovable);
pixitem->setPos(50,50);
QTransform trans;
trans.setMatrix(4.20399,2.9286,0,-3.86601,0.761397,0,33.1015,-134.5,1);
pixitem->setPixmap(QPixmap("abc.jpg").scaled(100,100));
pixitem->setTransform(trans);
pixitem->setShapeMode(QGraphicsPixmapItem::MaskShape);
scene->addItem(pixitem);
You can use setTransformationMode function to set the pixmap item's transformation mode. The default value is Qt::FastTransformation. You should set the transform mode to Qt::SmoothTransformation to enable bilinear filtering which results in a better quality when scaled :
pixitem->setTransformationMode(Qt::SmoothTransformation);
I'm getting a strange effect while scaling up a CCSprite loaded from a PVR texture. My original sprite is a simple solid color rectangle but I end up having a dithered color from both edges (left and right) with big scaling (ex: [mySprite setScaleX:100.0]). Note that this doesn't occur when I load my sprite from a standalone PNG file. Any idea what goes wrong or what special parameter could be missing ? BTW, I use TexturePacker and generating PNG textures is not better.
Thx.
[UPDATE] First image is the base sprite I want to expand ("test.png", real size is 3x30pix, here I zoomed 4 times for visibility purpose).
Second image is the expected result (the one I get when loaded from a standalone PNG file).
Code used:
aSprite = [CCSprite spriteWithFile:#"test.png"];
[self addChild:aSprite];
[aSprite setScaleX:300.0];
Third image is the result when loaded from the PVR texture.
Code used:
aSprite = [CCSprite spriteWithSpriteFrameName:#"test"];
[self addChild:aSprite];
[aSprite setScaleX:300.0];
Suppose I need several CCSprites using the same image. I can think of the 2 following solutions:
The image is in a separate file "bg.png"
CCSprite *image1 = [CCSprite spriteWithFile:#"bg.png"];
CCSprite *image2 = [CCSprite spriteWithFile:#"bg.png"];
The image is in a spritesheet "bg_sheet.png"
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"bg_sheet.png"];
Then
CCSprite *image1 = [CCSprite spriteWithSpriteFrameName:#"bg.png"];
CCSprite *image2 = [CCSprite spriteWithSpriteFrameName:#"bg.png"];
My questions are:
I guess that in case 1 the image is loaded twice in memory, whereas in case 2 it's loaded only once. Am I right ?
Then does it mean that it's ALWAYS better to use spritesheets?
Did I miss any other better way to achieve it?
You are not right. In both cases image will be placed into the memory only once. You can check spriteWithFile: code. It tries to find sprite frame in the sprite frame cache and load it only if there is no needed frame was found.
Using spritesheets help to save memory. For example, for image with size 129x129 will be created texture with size 256x256. But you can add many of such images in one spritesheet and only one big texture will be created( i mean, it there will be spritesheet 1024x1024 or 2048x2048 there will be only one texture with the same size).
I am going to set my image like that.. it looks like image A to B but i cant ... i want to change Image A with some and it becomes Image B...Its done in photoshop but in cocos2d..How can i do..Check images below...
Image A:
The Image B is:
How can i do this with sprite.rotation = -30?
or some other way?