image watermark on an image in android - java

i have an image in imageview , how can i add an image/logo watermark on that image.?
i have used following code to add text watermark
String watermark ="Sari collage";
int x =1500;
int y =1200;
int color = Color.parseColor("#ff720b");
int alpha=80;
int size = 70;
boolean underline= false;
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(watermark, x, y, paint);

I guess:
String watermark ="Sari collage";
int x =1500;
int y =1200;
int color = Color.parseColor("#ff720b");
int alpha=80;
int size = 70;
boolean underline= false;
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawBitmap(src, w/2, h/2, null);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(watermark, x, y, paint);

Related

How do I draw two lines that intersect on Bitmap - Java android

So I want to draw two lines that cross in the middle (like an x), and I want to draw this onto my randomly generated bitmap. Does anyone know how I can make the line diagonl?
public static Bitmap randomImage(int width, int height) {
Bitmap ranImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Random rand = new Random(System.currentTimeMillis());
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
int color = Color.rgb(r, g, b);
ranImage.setPixel(x, y, color);
}
//initialise a new canvas instance
Canvas canvas = new Canvas(ranImage);
//intialise a new paint instance to draw the line
Paint paint = new Paint();
//line color
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
//line width in pixels
paint.setStrokeWidth(3);
paint.setAntiAlias(true);
//set a pizel value to offset the line from the canvas edge
int offset = 50;
//draw a line on canvas at the center postion ....for now
canvas.drawLine(
offset,
canvas.getHeight()/2,
canvas.getWidth()-offset,
canvas.getHeight()/2,
paint
);
return ranImage;
}

Crop Bitmap image to custom sguare

Image sample
I have this method written for cropping my image, but HEIGHT dimension doesnt work on the cropping, only weight is done right. I cant find the issue. I want to use my screen width and height as a dynamic width and height.
public Bitmap cropToSquare(Bitmap bitmap){
int width = mScreenWidth ;
int height = mScreenHeight;
int newWidth = width - 2 * 10;
int newHeight = (height- newWidth) / 2;
Bitmap cropImg = Bitmap.createBitmap(bitmap, 0, 0, newWidth, newHeight);
return cropImg; }
}
Here is my solution:
public static Bitmap getResizedClippedBitmap (Bitmap bm, int newWidth, int newHeight) {
Bitmap targetBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
float originalWidth = bm.getWidth();
float originalHeight = bm.getHeight();
float scale, xTranslation = 0.0f, yTranslation = 0.0f;
if (originalWidth > originalHeight) {
scale = newHeight/originalHeight;
xTranslation = (newWidth - originalWidth * scale)/2.0f;
}
else {
scale = newWidth / originalWidth;
yTranslation = (newHeight - originalHeight * scale)/2.0f;
}
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(bm, transformation, paint);
return targetBitmap;
}
You can find some other utils here: BitmapUtils.java
Hope it helps.

Java SWT: Get subset of Pixels from Image to create a new Image

I am displaying an Image from a list of Pixels as follows (this works):
private Image GetImage()
{
PaletteData palette=new PaletteData(0xff0000,0x00ff00,0x0000ff);
ImageData imageData = new ImageData(bmpHW, bmpHW,32,palette);
currentImagePixelVec = getPixelsFromBMP(0, 0, graphicsMemory);
int pixelVecLoc=0;
for (int h = 0; h<bmpHW; h++)
{
for (int w = 0; w<bmpHW; w++)
{
int p = 0;
Pixel pixel = currentImagePixelVec.get(pixelVecLoc);
p = (pixel.Alpha<<24) | (pixel.Red<<16) | (pixel.Green<<8) | pixel.Blue;
imageData.setPixel(w, h, p);
pixelVecLoc++;
}
}
imageData = imageData.scaledTo(700, 700);
Image image = ImageDescriptor.createFromImageData(imageData).createImage();
return image;
}
I am getting the user to select a rectangle of the Image as follows(this works):
if(drag)
{
GC gc1 = e.gc;
//gc.setBackground(top.getDefault().getSystemColor(SWT.COLOR_BLACK));
gc1.setAlpha(128);
int minX = Math.min(startX, endX);
int minY = Math.min(startY, endY);
int maxX = Math.max(startX, endX);
int maxY = Math.max(startY, endY);
int width = maxX - minX;
int height = maxY - minY;
gc1.fillRectangle(minX, minY, width, height);
}
I would like to create a new image from the rectangle selection:
private Image GetZoomedImage()
{
int minX = Math.min(startX, endX);
int minY = Math.min(startY, endY);
int maxX = Math.max(startX, endX);
int maxY = Math.max(startY, endY);
int width = maxX - minX;
int height = maxY - minY;
PaletteData palette=new PaletteData(0xff0000,0x00ff00,0x0000ff);
ImageData imageData = new ImageData(1300, 1300,32,palette);
int newHeight = 0;
int newWidth = 0;
for (int h = minX; h<maxX; h++)
{
for (int w = minY; w<maxY; w++)
{
int p = 0;
//Pixel pixel = currentImagePixelVec.get((h * w)-1);
int actualPix = imageDisplayed.getImageData().getPixel(h, w);
//p = (pixel.Alpha<<24) | (pixel.Red<<16) | (pixel.Green<<8) | pixel.Blue;
System.out.println("Pixel: " + Integer.toString(actualPix));
//imageData.setPixel(newWidth,newHeight, p);
imageData.setPixel(newWidth,newHeight, actualPix);
newWidth++;
}
newHeight++;
}
imageData = imageData.scaledTo(700, 700);
Image image = ImageDescriptor.createFromImageData(imageData).createImage();
return image;
}
Am I on the right track on this one?
You should only get the image data of the existing image once as this can be an expensive operation:
ImageData imageDisplayedData = imageDisplayed.getImageData();
Your new image data should be just the width and height you want:
ImageData imageData = new ImageData(width, height, 32, palette);
You can use the
public void getPixels(int x, int y, int getWidth, int[] pixels, int startIndex)
public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex)
methods of ImageData to read and write the data for a whole row at a time:
int [] pixels = new int [width];
int newRow = 0;
for (int row = minY; row < maxY; row++)
{
imageDisplayedData.getPixels(minX, row, width, pixels, 0);
imageData.setPixels(0, newRow, width, pixels, 0);
newRow++;
}

How to highlight specific rectangles on canvas

Based on the code I've used for my canvas drawing and the screenshot regarding that, I'm trying to fill a specific red rectangle but it's not working. What can be done to ensure that ONLY the rectangle on the top row, 2nd from left is filled?
public class Car extends View {
public Car(Context context) {
super(context);
init();
}
public Car(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Car(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
Paint paint;
private void init() {
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(4);
// paint.setStyle(Paint.Style.STROKE); // delete line for filled rect
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int w = canvas.getWidth();
int h = canvas.getHeight();
int rectWidth = w / 5;
int space = w / 15;
int topRectHeight = getPaddingTop();
int bottomRectHeight = getPaddingBottom();
for (int i = 0; i < 4; i++) {
int left = i * (rectWidth + space);
int right = left + rectWidth;
if (i == 2){
paint.setStyle(Paint.Style.STROKE); // delete line for filled rect
}
Rect rect = new Rect(left, 0, right, topRectHeight);
canvas.drawRect(rect, paint);
Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
canvas.drawRect(rect2, paint);
}
}
}
This way only the specified rectangle will be filled (only top 2nd one).
paint.setStyle(Paint.Style.STROKE); //add this
for (int i = 0; i < 4; i++) {
int left = i * (rectWidth + space);
int right = left + rectWidth;
if (i == 1){
paint.setStyle(Paint.Style.FILL); // change to this
}
Rect rect = new Rect(left, 0, right, topRectHeight);
canvas.drawRect(rect, paint);
paint.setStyle(Paint.Style.STROKE);//add this
Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
canvas.drawRect(rect2, paint);
}
If you want only the 2nd bottom to be filled:
paint.setStyle(Paint.Style.STROKE); //you can remove this now
for (int i = 0; i < 4; i++) {
paint.setStyle(Paint.Style.STROKE);//add this
int left = i * (rectWidth + space);
int right = left + rectWidth;
Rect rect = new Rect(left, 0, right, topRectHeight);
canvas.drawRect(rect, paint);
if (i == 1){
paint.setStyle(Paint.Style.FILL); // change to this
}
Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
canvas.drawRect(rect2, paint);
}
And if you want both the 2nd top and bottom filled:
paint.setStyle(Paint.Style.STROKE); //you can remove this now
for (int i = 0; i < 4; i++) {
paint.setStyle(Paint.Style.STROKE);//add this
int left = i * (rectWidth + space);
int right = left + rectWidth;
if (i == 1){
paint.setStyle(Paint.Style.FILL); // change to this
}
Rect rect = new Rect(left, 0, right, topRectHeight);
canvas.drawRect(rect, paint);
Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
canvas.drawRect(rect2, paint);
}

Android Circular image with pointer

I am developing Android application, in my application I have to show the user profile image as shown in this image. I had used CircularImage but I need to have the pointer in the bottom, can anyone help me how to create circular image with pointer the bottom
you can set your pointer(blue background) as background for ImageView and set user image as src in bitmap
also you need to round your bitmap,by this method you can earn circular bitmap:
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2.0f;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2.0f;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
and set round bitmap as src for your ImageView:
imageView.setImageBitmap(toRoundBitmap(mBitMap));
for better view set android:scaleType="fitStart" in your ImageView tag in xml

Categories

Resources