How to XOR two images in android? - android

I want to XOR two images in android as iam working on image encryption app iam bringing images from SD card and loading them in image view now as i have loaded two images i want to XOR both of them

Another alternative would be to draw on a Canvas both of your bitmaps. One bitmap does not specify any settings, but the other should specify a PorterDuffXfermode to Mode.XOR, in his Paint object.
Ex:
ImageView compositeImageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.batman_ad);
Bitmap bitmap2=BitmapFactory.decodeResource(getResources(), R.drawable.logo);
Bitmap resultingImage=Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());
Canvas canvas = new Canvas(resultingImage);
// Drawing first image on Canvas
Paint paint = new Paint();
canvas.drawBitmap(bitmap1, 0, 0, paint);
// Drawing second image on the Canvas, with Xfermode set to XOR
paint.setXfermode(new PorterDuffXfermode(Mode.XOR));
canvas.drawBitmap(bitmap2, 0, 0, paint);
compositeImageView.setImageBitmap(resultingImage);

It depends what you want to xor, the pixels or data itself.
any way an easy way to do it is to convert the to images to array of all the pixels, XOR them together and then convert it back to a bitmap. NOTE that this example will only work to bitmap with identical resolutions.
//convert the first bitmap to array of ints
int[] buffer1 = new int[bmp1.getWidth()*bmp1.getHeight()];
bmp1.getPixels(buffer1,0,bmp1.getWidth(),0,0,bmp1.getWidth(),bmp1.getHeight() );
//convert the seconds bitmap to array of ints
int[] buffer2 = new int[bmp2.getWidth()*bmp2.getHeight()];
bmp2.getPixels(buffer2,0,bmp2.getWidth(),0,0,bmp2.getWidth(),bmp2.getHeight() );
//XOR all the elements
for( int i = 0 ; i < bmp1.getWidth()*bmp1.getHeight() ; i++ )
buffer1[i] = buffer1[i] ^ buffer2[i];
//convert it back to a bitmap, you could also create a new bitmap in case you need them
//for some thing else
bmp1.setPixels(buffer1,0,bmp1.getWidth(),0,0,bmp2.getWidth(),bmp2.getHeight() );
see:
http://developer.android.com/reference/android/graphics/Bitmap.html

Related

Android: Efficiently draw on images

The helper function below draws a rectangle on top of input bitmap and returns a thumbnail bitmap. However, I run into java OutOfMemory Error when I call this helper around 1000 times from an activity to populate a list of thumbnails. I tried resizing the tempScaledBitmap to 375, 500 but the quality of the thumbnail image is poor.
Also, I was unable to directly draw on inputBitmap as it was immutable.
What is an efficient way to display a list of 1000 plus thumbnails in an activity?
private static Bitmap drawOnCanvas(Bitmap inputBitmap, FramePoint[] points, ColorCode colorCode){
Bitmap tempScaledBitmap = Bitmap.createScaledBitmap(inputBitmap, 750, 1000, false);
//draw path
Canvas canvas = new Canvas(tempScaledBitmap);
// Path
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(colorCode.equals(ColorCode.GREEN)?Color.GREEN:(colorCode.equals(ColorCode.RED)?Color.RED:Color.BLUE));
paint.setStrokeWidth(5);
Path p = new Path();
p.moveTo(points[0].getPointX(), points[0].getPointY());
p.lineTo(points[1].getPointX(), points[1].getPointY());
p.lineTo(points[2].getPointX(), points[2].getPointY());
p.lineTo(points[3].getPointX(), points[3].getPointY());
p.close();
canvas.drawPath(p, paint);
return tempScaledBitmap;
}
After each call to drawOnCanvas you should recycle your bitmaps:
inputBitmap.recycle();
Also recycle the tempScaledBitmap after they are assigned to ImageViews or wherever to prevent this OOM errors.
Also, you could consider using picasso or glide. For me, it's the best option of all.

Changing image color takes too much time in android

I am trying to change image color dynamically in android .but it takes to much time .
Below function is used for changing color.
public void greenColor(ImageView imageView,String fileName){
System.out.println("in green color method");
//initialize the Bitmap Object
Bitmap bmp = BitmapFactory.decodeFile(fileName);
// Bitmap bmp = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
//Guarantees that the image is decoded in the ARGB8888 format
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
//Initialize the intArray with the same size as the number of pixels on the image
int[] intArray = new int[bmp.getWidth()*bmp.getHeight()];
//copy pixel data from the Bitmap into the 'intArray' array
bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
//replace the red pixels with yellow ones
try{
for (int i=0; i < intArray.length; i++)
{
//System.out.println("pixel value :"+intArray[i]);
//intArray[i] = 0xFFFF0000;
if(intArray[i] == Color.WHITE)
{
System.out.println("color white ");
} else{
System.out.println(intArray[i]);
intArray[i]=Color.GREEN;
}
}
}catch(Exception e){
System.out.println("pixel error "+e);
}
//Initialize the bitmap, with the replaced color
bmp = Bitmap.createBitmap(intArray, bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
//Draw the bitmap with the replaced color
imageView.setImageBitmap(bmp);
//----end color
}
I've tried so much and did google search, but i am unable to solve my problem ,
Is there any technique to reduce image color change time in android?
Please help me ...
in your case its taking time as you are converting the image into 2d array and checking at every index.
So if image if 2000*2000 size, just imagine how much you are processing.
If you want to change the alfa or tint then you should use the Bitmap APIs instead of doing it manually.

Android:Draw image in the center of another image

I have one image image 1 and one is coming from server that is image 2 i am trying to draw second one just at the center of the first. as result i want single image like in pic .
This should do what you're looking for:
The backgroundBitmap variable would be your image1 and the bitmapToDrawInTheCenter would be your image2.
public void centerImageInOtherImage()
{
Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap bitmapToDrawInTheCenter = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);
Bitmap resultingBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), backgroundBitmap.getConfig());
Canvas canvas = new Canvas(resultingBitmap);
canvas.drawBitmap(backgroundBitmap, new Matrix(), null);
canvas.drawBitmap(bitmapToDrawInTheCenter, (backgroundBitmap.getWidth() - bitmapToDrawInTheCenter.getWidth()) / 2, (backgroundBitmap.getHeight() - bitmapToDrawInTheCenter.getHeight()) / 2, new Paint());
ImageView image = (ImageView)findViewById(R.id.myImage);
image.setImageBitmap(resultingBitmap);
}
Courtesy : Draw text/Image on another Image in Android
Drawing images over each other is fairly simple with Canvas. Canvas basically acts as the drawing board to draw text/Image. You just need to construct a canvas with the first Image and then draw the second Image at the center as shown below
/* This ImageOne will be used as the canvas to draw an another image over it. Hence we make it mutable using the copy API
as shown below
*/
Bitmap imageOne = BitmapFactory.decodeResource(getResources(), R.drawable.imageOne).copy(Bitmap.Config.ARGB_8888,true);
// Decoding the image two resource into a Bitmap
Bitmap imageTwo= BitmapFactory.decodeResource(getResources(), R.drawable.imageTwo);
// Here we construct the canvas with the specified bitmap to draw onto
Canvas canvas=new Canvas(imageOne);
/*Here we draw the image two on the canvas using the drawBitmap API.
drawBitmap takes in four parameters
1 . The Bitmap to draw
2. X co-ordinate to draw from
3. Y co ordinate to draw from
4. Paint object to define style
*/
canvas.drawBitmap(imageTwo,(imageOne.getWidth())/2,(imageOne.getHeight())/2,new Paint());
imageView.setImageBitmap(imageOne);

How can i merge two bitmap one over another at selected point on the first image in android?

How can i merge two different images as one. Also i need to merge the second image at a particular point on the first image. Is it posible in android??
This should work:
Create a canvas object based from the bitmap.
Draw another bitmap to that canvas object (methods will allow you
specifically set coordinates).
Original Bitmap object will have new data saved to it, since the
canvas writes to it.
I guess this function can help you:
private Bitmap mergeBitmap(Bitmap src, Bitmap watermark) {
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas cv = new Canvas(newb);
// draw src into canvas
cv.drawBitmap(src, 0, 0, null);
// draw watermark into
cv.drawBitmap(watermark, null, new Rect(9, 25, 154, 245), null);
// save all clip
cv.save(Canvas.ALL_SAVE_FLAG);
// store
cv.restore();
return newb;
}
It draws the water mark onto "src" at specific Rect.

Android combine pictures

Hy, I tried to search this but not very much luck
the most similar is this one
http://ketankantilal.blogspot.com/2011/03/how-to-combine-images-and-store-to.html
Anyhow, I'm developing for android.
The question is I have images in png format (or jpg as bmp is quite large for my app).
How can I combine three images from top to bottom.
I do not need to save them on sd just to display them.
Thanks, and sorry if a similar question with answer exists.
You could use a Canvas and then draw each Bitmap (assuming each image is loaded into a Bitmap object) using appropriate top and left offsets.
You would increase the top offset of the next bitmap by the total size of the previously drawn Bitmaps.
Check out http://developer.android.com/reference/android/graphics/Canvas.html
example:
public void stackImages(Context ctx)
{
// base image, if new images have transparency or don't fill all pixels
// whatever is drawn here will show.
Bitmap result = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
// b1 will be on top
Bitmap b1 = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);
// b2 will be below b1
Bitmap b2 = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
c.drawBitmap(b1, 0f, 0f, null);
// notice the top offset
c.drawBitmap(b2, 0f, 200f, null);
// result can now be used in any ImageView
ImageView iv = new ImageView(ctx);
iv.setImageBitmap(result);
// or save to file as png
// note: this may not be the best way to accomplish the save
try {
FileOutputStream out = new FileOutputStream(new File("some/file/name.png"));
result.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}

Categories

Resources