I have an android project to process images, I have two images, I want to overlay one over another (blending). To combine these two images, it’s a bit simple, using drawing on canvas:
I used this code:
public static Bitmap overlay(Bitmap bottomImg, Bitmap topImg) {
Bitmap bmOverlay = Bitmap.createBitmap(bottomImg.getWidth(),bottomImg.getHeight(),bottomImg.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bottomImg, new Matrix(), null);
canvas.drawBitmap(topImg, 0, 0, null);
return bmOverlay;
}
It works well, but It doesn’t manipulate the transparency of overlaed image. Actually I want the top image to be transparent, in order to see how it matches the bottom one. It’s something to access alpha channel or to do it manually, Can someone help me to do this task.
You can try :
topImg.eraseColor(Color.TRANSPARENT);
This will set all the pixels to transparent.
Related
I am writing a game program for educational purposes. I'm using the SurfaceView class. The program displays the images on the screen using the Canvas object. The drawable folder contains a png-image. By the name of the picture "bitmapName" I get the Bitmap using this code
int resID = context.getResources().getIdentifier(bitmapName, "drawable", context.getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
I use the drawBitmap() method to draw the resulting Bitmap to the screen. The problem is that the object on which this picture is superimposed is used in the program quite often and the picture should have different shades depending on the logic. The image is now yellow, then green, and other shades.
And there are a LOT of shades. The easiest way to do this is simply to create images of different shades in advance and load them into memory. But uploading a lot of pictures will greatly increase the size of the application, and I would not want this. I tried this in Photoshop. I put a layer on top of the image and filled it with a solid color. Lowered the opacity of this layer. The result is a picture that looks like what I want. Therefore, the question is: how to create a Bitmap from another ready-made Bitmap and some color with transparency.
I need a method to which I send Bitmap, color, transparency level; and get a ready-made Bitmap of a changed shade from it. Can you tell me how can I achieve this result? How to overlay color on Bitmap?
I have been trying to blur an imageview and set the background of the whole layout to the blurred bitmap to no avail. Basically what am trying to achieve is shown in these pictures, where they only blur the imageView and set the expanded blurred image as the layout background.
int color = getDominantColor(bitmap); //get dominant color of the bitmap
//create gradient
GradientDrawable gradient = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xf5f5f5,color});
gradient.setCornerRadius(0f);
//setbackground of the relativelayout
rl.setBackground(gradient);//rl is relative layout
//getting dominant color of bitmap
public static int getDominantColor(Bitmap bitmap) {
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
final int color = newBitmap.getPixel(0, 0);
newBitmap.recycle();
return color;
}
I dont want tp set the background as a gradient, but as the blurred result of that image bitmap, just like in the images i've included.
There are many options:
You can enlarge and then downscale the bitmap. What that does is decrease the number of pictures producing something like the effect of blurring. See here if you want to do that: Fast Bitmap Blur For Android SDK. However, it isn't very memory efficient and you may want to change your implementation if it works like this.
Use a pre-made library. There are many libraries, some better than others, allowing you to blur bitmaps. The people who made these libraries have already done the hard job at reducing memory usage and better compatibility. An example is also here: Fast Bitmap Blur For Android SDK
Note: I didn't post any code because part of the solution is libraries or custom classes.
Hope it helps.
I want to load some images to the cloud, but I want to add some protections when someone views the photos and saves the images; they will not see anything because of the transparency.
would the code be common for Java and Android? I would like to prototype it in Java first.
I have found some code that combines two files. One file is my main file the other is a transparent file. The combined file does not have a transparent overlay.
Do I need to use an image drawing order?
http://www.developer.nokia.com/document/Java_Developers_Library_v2/GUID-D3E35E6F-0C45-48ED-B09D-F716E14C1C02/javax/microedition/amms/control/imageeffect/OverlayControl.html
BufferedImage image = ImageIO.read(new File("rose.jpg"));
BufferedImage overlay = ImageIO.read(new File("myimg1.gif"));
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
// Save as new image
ImageIO.write(combined, "PNG", new File("combined.png"));
This won't work. If they can see the image, they can copy it, one way or another. A solution to consider is providing watermarked thumbnails at no charge, then only offering the full resolution image for a fee. However, they can still copy the full resolution image once they pay.
You can actually put a trans image as an overlay to the orginial image, that will
Protect from download, I think this is usually done by the server side aka your cloud
I know from some websites that they use some kind of an overlay such as yours
And the browser can't see the image below so you can't download.
I actually didn't understand how you implementing this - the image is opened in the browser?
Just a wild though, you can also cut the image into pieces like a jigsaw puzzle
The device won't have problems connecting it togther but when you download you'll
Download only "one piece of the puzzle" :-P
I am trying to make a composite image from a camera preview and an ImageView that was above it. I have one image that is a transparent png which is set on an imageview like this
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.one);
I then add it to the framelayout which is already showing my camera preview (inherits SurfaceView) like so:
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(cp); //cp is a reference to a camera preview object
preview.addView(iv);
My imageview's picture is this:
And the screen is something like this (I had to take a the pic from another camera since the DDMS screenshot wasn't showing the preview only the image and a black screen, don't know if that's a relevant though):
Now my task is to take that picture with the imageview. I came up with two approaches, both of which I do not know whether they can or cannot be implemented
Save the picture seperately, keep track of which cover was on the image and then merge someway. Can this be done, and how?
Gain the look of the framelayout in which both Views are residing and save as image
Take screenshot of a specific area, I will onky do this one as a last resort i.e. if this can be done
What I want to know is which one these approaches is possible and how can it be done? or is there a better way to get this done?
Assuming you already have image in form of byte[] data from jpeg callback.
Decode the image into a mutable bitmap:
Bitmap photo = BitmapFactory.decodeByteArray(data, 0, data.length);
photo = photo.copy(photo.getConfig(), true);
Read the overlay:
Bitmap overlay = BitmapFactory.decodeResource(getResources(), R.drawable.one);
Draw overlay on the photo:
Canvas canvas = new Canvas(photo);
canvas.drawBitmap(overlay, new Matrix(), null);
Now, photo should contain your image.
In a feeble attempt to learn some Android development am I stuck at graphics. My aim here is pretty simple:
Take n small images and build a random image, larger than the screen with possibility to scroll around.
Have an animated object move around on it
I have looked at the SDK examples, Lunar Lander especially but there are a few things I utterly fail to wrap my head around. I've got a birds view plan (which in my head seems reasonably sane):
How do I merge the tiles into one large image?
The background is static so I figure I should do like this:
Make a 2d array with refs to the tiles
Make a large Drawable and draw the tiles on it
At init draw this big image as the background
At each onDraw redraw the background of the previous spot of the moving object, and the moving object at its new location
The problem is the hands on things. I load the small images with "Bitmap img1 = BitmapFactory.decodeResource (res, R.drawable.img1)", but then what? Should I make a canvas and draw the images on it with "canvas.drawBitmap (img1, x, y, null);"? If so how to get a Drawable/Bitmap from that?
I'm totally lost here, and would really appreciate some hands on help (I would of course be grateful for general hints as well, but I'm primarily trying to understand the Graphics objects). To make you, dear reader, see my level of confusion will I add my last desperate try:
Drawable drawable;
Canvas canvas = new Canvas ();
Bitmap img1 = BitmapFactory.decodeResource (res, R.drawable.img1); // 50 x 100 px image
Bitmap img2 = BitmapFactory.decodeResource (res, R.drawable.img2); // 50 x 100 px image
canvas.drawBitmap (img1, 0, 0, null);
canvas.drawBitmap (img2, 50, 0, null);
drawable.draw (canvas); // obviously wrong as draw == null
this.setBackground (drawable);
Thanks in advance
I finally figured it out, what I need for the basic problem of tiling a bunch of Bitmaps together was a BitmapDrawable:
Bitmap myImage = Bitmap.createBitmap(450, 300, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(myImage);
Bitmap img1 = BitmapFactory.decodeResource (res, R.drawable.img1); // 50 x 100 px image
Bitmap img2 = BitmapFactory.decodeResource (res, R.drawable.img2); // 50 x 100 px image
canvas.drawBitmap (img1, 0, 0, null);
canvas.drawBitmap (img2, 50, 0, null);
this.setBackgroundDrawable(new BitmapDrawable(myImage));
I obviously had it backwards with the relationship between Canvas and Bitmap, I thought Canvas was a blank screen on which to paint on. Apparently (if I got it right this time) it's connected to a Bitmap, and if having that one it's possible to use it.
I still wouldn't know how to get it in the onDraw(Canvas) function though, but this solves the problem I had.