Wrapping image around object in android - android

I'm creating a app in android for mugs like this
I know how to do everything except wrapping an image around an object. It would be a simple task to do if I would only have to stack an image on top of the other, if they were flat, but if it is a round object, as this mug is, it's kinda tricky.
I thought of creating a obj object file in blender and render the image on it in OpenGL or Unity. The obj file size goes to 5MB. And i cannot use this much big file in my app.
Is there any alternate good approach for it? If there please share a reference or example for it

Use in this manner
the work around of it by doing a masking over the image
ImageView mImageView= (ImageView)findViewById(R.id.imageview_id);
Bitmap original = BitmapFactory.decodeResource(getResources(),R.drawable.content_image);
Bitmap mask = BitmapFactory.decodeResource(getResources(),R.drawable.mask);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint); paint.setXfermode(null);
mImageView.setImageBitmap(result);
mImageView.setScaleType(ScaleType.CENTER);
mImageView.setBackgroundResource(R.drawable.background_frame);
As mentioned here Masking(crop) image in frame

I suppose that this is the right way, but using pure OpenGL is difficult. I used LibGDX for simple 3d modeling.

Related

How to draw an image on top a bitmap and combine as one bitmap in Android Java

I am developing an Android application using Java. I am doing some image manipulating in my application. First of all, I like to save I have no knowledge about image processing. But, I am trying to get into it. What I would like to do now is draw a simple image on a bitmap and save it as one bitmap.
I am loading an image from asset folder as a bitmap like this.
Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Let's say the photo is a just simple rectangle like this.
Then I would like to draw a bitmap (triangle shape) using coordinates point. The image would be some this.
My imagination of code would be like this.
rectBitmap.drawOnTop(coorPointOneValues, coorPointTwoValues, coorPointThreeValues);
Coordinate point values would be x and y value since I am working on the 2D coordinate system.
Then I would like to save the image something like this after drawing traingle
rectBitmap = rectBitmap.saveBitmap();
How can I do it? The scenario mentioned is a possible way that I can think of. If it is not possible, what would be the other way around?
You can do it like this.
1, read or create your bitmap:
Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2, create a canvas on the bitmap:
Canvas canvas = new Canvas(bitmap);
3, draw something:
canvas.drawColor(Color.RED)
canvas.drawRect / canvas.drawLine / canvas.drawArc ...
//for triangle shape you can use drawPath
4, save the bitmap:
bitmap.compress(CompressFormat format, int quality, OutputStream stream)

transform a sqaure shape drawable in to circular shape drawable withaout affecting the pixel quality

I have an application which takes picture from camera .And normally this pictures have either rectangle shapes or square shapes.But due to make these pictures circular in shape i limited the shape to only square.So now my problem is to make this square picture into a circular shape having radius of half of width or height.And the clipped part should be removed from the picture.
Its same like making a circle from the square which touches midpoint of all of the faces of square.
I don't have any experience with canvas or other in built drawing functions.
For example i have square at first now i want to make image circular such that parts pointed by arrow should get clipped from image.
Note:as i have to work with camera images .quality is crucial factor.So please suggest the possible ways that will not affect the pixel quality and other important factors.
You can do it by masking the image with your desired shape, see this post for detailed info.
example code:
Resources resources = context.getResources();
Bitmap original = BitmapFactory.decodeResource(resources,R.drawable.original);
Bitmap mask = BitmapFactory.decodeResource(resources,R.drawable.mask);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
c.drawBitmap(original, 0, 0, null);
c.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
imageView.setImageBitmap(result);

Android: What's the opposite of Bitmap.extractAlpha()

I need to do something which exactly the opposite of Bitmap.extractAlpha: Apply an alpha map (which loaded from a file) onto a RGB bitmap (which also loaded from a file).
Yes! Just like "CGImageCreateWithMask" in iOS!
The Square blog had a tutorial about this just last week: http://corner.squareup.com/2013/01/transparent-jpegs.html :).
I dont know what exactly CGImageCreateWithMask does, but if you want another picture to serve as the alpha channel for your Bitmap, you can create one as described in this question, which combines four images. I haven't tried it now, but I think for two colors it would look something like this:
Paint colorPaint = new Paint();
redPaint.setShader(new BitmapShader(redChanImg, TileMode.CLAMP, TileMode.CLAMP));
Paint alphaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
alphaPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
c.setBitmap(resultImage);
c.drawRect(0, 0, width, height, colorPaint);
c.drawBitmap(alphaImg, 0, 0, alphaPaint);
// save result somewhere
You can also always just work on the raw pixel data of a bitmap using Bitmap.getPixels

Trouble making parts of a bitmap transparent

I'm developing an android app where i need to capture text and save it as a transparent image. Capturing the text has been done but making a transparent png file is where i'm stuck as i'm not familiar with image pixel manipulation at all. Here's what I have so far... i first create a blank bitmap and fill it with a white background, then i set the paint's transparency to 0 (full transparency) and then draw the source bitmap into the destination bitmap using the XOR modes.. but when i run the app all i see is a blank white image. i'll be glad if someone points out what i'm doing wrong and how to fix it. Thanks in advance.
b = Bitmap.createBitmap(tw, th,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
Rect dest = new Rect(0,0,b.getWidth(),b.getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, b.getWidth(), b.getHeight(), paint);
paint.setAlpha(0);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawBitmap(bmp,null,dest,paint);
Have you looked at : How to change a bitmap's opacity?
Seems like
paint.setAlpha(0);
won't do anything as you need to set the alpha channel to something greater than 0...
Use:
Color.argb(0,0,0,0)
The first parameter is the alpha. Set it to 0 for complete transparency.

what is the relation between Canvas and Bitmap?

What is the relation between Canvas and Bitmap?
Bitmap drawingBitmap = Bitmap.createBitmap(bmp1.getWidth(),
bmp1.getHeight(), bmp1.getConfig());
canvas = new Canvas(drawingBitmap);
paint = new Paint();
canvas.drawBitmap(bmp1, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SCREEN));
canvas.drawBitmap(bmp2, 0, 0, paint);
compositeImageView.setImageBitmap(drawingBitmap);
I don't understand this code.Why the drawingBitmap is the composition of bmp1 and bmp2?
Basically, the Canvas is backed by a Bitmap, so when you draw anything using the canvas, the canvas will draw into the Bitmap it was created with. So, when you draw those two bitmaps using the canvas, it's going to composite the bitmaps together and the result will be stored in drawingBitmap, as it's backing the canvas.
Anh's analogy is correct-ish, though probably confusing (and over-simplifying, which I'm also doing above) – as I mentioned in a comment, you can think of the Canvas as the pen, the Paint as a configuration of that pen (e.g., replaceable ink or something - whatever you can fit into the idea of a configurable pen), and the Bitmap as the paper you draw onto. The analogy becomes confusing if you focus too much on the accepted meaning of the words.
Let's think canvas as a pen, and drawingBitmap as a paper. You use your pen to draw something on your paper, and you get what you draw.
Technically, you can construct Canvas object from Bitmap to draw others bitmaps on it.
Canvas is the place or medium where perfroms/executes the operation of drawing, and Bitmap is responsible for storing the pixel of the picture you draw.

Categories

Resources