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.
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 get the portion of an image that is visible on the screen in an image view. Like if there is an image zoomed in on the image view, then the zoomed in portion visible on the screen should be cropped out and stored.
I can not use libraries or camera intents for the task
. All has to be done on the same page. Consider i have the image in bitmap/imageview/resource file.
Use these lines for the problem
Bitmap result = Bitmap.createBitmap(yourImageView.getWidth(),
yourImageView.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(result);
yourImageView.draw(c);
These 4 lines of code will give you what you exactly see on screen in the Bitmap result.
I'm new to Android Java programming. For a project I need transparent images, which change dynamically. I have created a layout with an ImageView. And in it a small GIF USA flag GIF with a transparent background.
In code I connect the variable to the imageview and assign the same image to the variable from the ImageResources
ImageView image = (ImageView)convertView.findViewById(R.id.listview_image);
image.setImageResource(R.drawable.vlag);
This goes pretty well however the image is displayed with a white background. The transparency is lost. This is shown in the image below by the white background.
GIF over ImageView are always an issue. Use GIFImageView
And you must use it this way:
gifImageView = (GifImageView) findViewById(R.id.gifImageView);
gifImageView.setBytes(bytes);
gifImageView.startAnimation();
Even it has an option to download the gif from the web.
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.