Clear circle Bitmap - android

I have loaded a floor plan image and converted it into a Bitmap. And I have a circle drawn onto that floor plan image. This circle shows the current position of this user on the floor plan.
The problem is, when it comes a slow response from the server, multiple circle are drawn on the floor plan
Here is my code :
public void onServiceUpdate(ServiceState state) {
final int i, j;
i = state.getImagePoint().getI();
j = state.getImagePoint().getJ();
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
final ImageView imageFloor = (ImageView) findViewById(R.id.imageView);
final Bitmap bitmapCircle = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
Canvas canvas = new Canvas(bitmapCircle);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
canvas.drawBitmap(bitmap, new Matrix(), null);
canvas.drawCircle(i, j, 10, paint);
runOnUiThread(new Runnable() {
#Override
public void run() {
imageFloor.setImageBitmap(bitmapCircle);
}
});
and this is method calling floor plan image from indooratlas server.
public void loadFloorPlanImage(FloorPlan floorPlan) {
BitmapFactory.Options options = createBitmapOptions(floorPlan);
FutureResult<Bitmap> result = mIndoorAtlas.fetchFloorPlanImage(floorPlan,options);
result.setCallback(new ResultCallback<Bitmap>() {
#Override
public void onResult(final Bitmap result) {
runOnUiThread(new Runnable() {
#Override
public void run() {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(result);
log("onResult LoadFloorPlanImage");
}
});
}
is there any ideas or some advice how to clear the circle inside floor plan bitmap ?

You could do like this:
onServiceUpdate(...){
final ImageView imageFloor = (ImageView) findViewById(R.id.imageView);
imageFloor.setVisibility(View.GONE);
....
runOnUiThread(new Runnable() {
#Override
public void run() {
imageFloor.setVisibility(View.VISIBLE);
imageFloor.setImageBitmap(bitmapCircle);
But your question isn't very clear, I would not be surprised if I didn't understand what you meant

Related

Android ImageView to Bitmap after rotating

I want to create a bitmap from an imageView, but I first need to rotate my imageView before converting the bitmap. I use the following code to convert the imageView to Bitmap.
BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
Bitmap photo = drawable.getBitmap();
The imageView implements a rotation gesture. I want to be able to rotate the imageView and then create a Bitmap using the rotated imageView. However, it seems my Bitmap is still getting the original imageView instead of the rotated position. The following is the complete code. Thank you.
public class MainActivity extends AppCompatActivity implements RotationGestureDetector.OnRotationGestureListener{
private ImageView imgPhoto, imgBackground, imgCombine;
private RotationGestureDetector mRotationDetector;
private Button btnCombine;
private float angle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRotationDetector = new RotationGestureDetector(this);
imgPhoto = (ImageView) findViewById(R.id.imgPhoto);
imgBackground = (ImageView) findViewById(R.id.imgBackground);
collageImage = (ImageView) findViewById(R.id.imgCombine);
btnCombine = (Button)findViewById(R.id.btnCombineImage);
btnCombine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
Bitmap photo = drawable.getBitmap();
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event){
mRotationDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public void OnRotation(RotationGestureDetector rotationDetector) {
angle = rotationDetector.getAngle();
Log.d("RotationGestureDetector", "Rotation: " + Float.toString(angle));
imgPhoto.setRotation(imgPhoto.getRotation() + (-angle));
}
}
To rotate ImageView:
Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);
Source: https://stackoverflow.com/a/10104318/7639113
Then you can create the bitmap by using DrawingCache,
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
Try using this code after the onClick. It should provide a rotated bitmap with the angle specified.
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotatedPhoto = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
The rotate effect is actually applied at the Canvas level. So, what you can do is get the current Canvas augmentations and then draw your Bitmap into a new one with the changes.
Matrix transformMatrix = imgPhoto.getImageMatrix()
Bitmap original = photo;
Bitmap adjusted = Bitmap.createBitmap(original.getWidth(),
original.getHeight(),
original.getConfig());
Canvas canvas = new Canvas(adjusted);
canvas.setMatrix(transformMatrix);
canvas.drawBitmap(original, 0, 0, null);
//at this point adjusted bitmap contains the rotated image

How to corp a picture into any shape via a template in android?

I wanted to know if it was possible to crop a picture to any other shape not just square, rectangle or circle.
Basically what I am looking for is that, the user can select a template of a png file (already present) and it cuts the picture in that shape.
Check out this code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageViewPreview = (ImageView) findViewById(R.id.imageview_preview);
new Thread(new Runnable() {
#Override
public void run() {
final Bitmap source = BitmapFactory.decodeResource(MainActivity.this.getResources(),
R.drawable.source);
final Bitmap mask = BitmapFactory.decodeResource(MainActivity.this.getResources(),
R.drawable.mask);
final Bitmap croppedBitmap = cropBitmap(source, mask);
runOnUiThread(new Runnable() {
#Override
public void run() {
imageViewPreview.setImageBitmap(croppedBitmap);
}
});
}
}).start();
}
private Bitmap cropBitmap(final Bitmap source, final Bitmap mask){
final Bitmap croppedBitmap = Bitmap.createBitmap(
source.getWidth(), source.getHeight(),
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(croppedBitmap);
canvas.drawBitmap(source, 0, 0, null);
final Paint maskPaint = new Paint();
maskPaint.setXfermode(
new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
canvas.drawBitmap(mask, 0, 0, maskPaint);
return croppedBitmap;
}
}
The main function is the "cropBitmap" function. Basically it receives two bitmaps, a source and a mask, and then it "crops" the source bitmap using the mask's shape.
This is my source bitmap:
This is the mask bitmap:
And this is the result:
Also, check out this great presentation, this might help you too: Fun with Android shaders and filters

Android with opencv - image to grayscale

I'm looking for simple example of app in Android which convert color image to grayscale using opencv.
I'm trying with thic code, but app crash after this line Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img=(ImageView)findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
Utils.bitmapToMat(bmp, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY,4);
Utils.matToBitmap(tmp, bmp);
img.setImageBitmap(bmp);
}
Real quick way to do it. Without opencv.
private Button button;
private ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
image = (ImageView)findViewById(R.id.imageView1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
image.setColorFilter(filter);
}
});
I just tried it, works for me.
edit: Check out the link here for official doc.
http://developer.android.com/reference/android/graphics/ColorMatrix.html#setSaturation(float)
Your layout has not been laid out yet, while you are in onCreate(), that is why bmp.getWidth() and bmp.getHeight() return 0.
One way to do it is to override onWindowFocusChanged()
private ImageView img;
private boolean isFullyInitialized = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView1);
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if (hasFocus && !isFullyInitialized)
{
isFullyInitialized = true;
openCvCode();
}
}
private void openCvCode()
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
Utils.bitmapToMat(bmp, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY,4);
Utils.matToBitmap(tmp, bmp);
img.setImageBitmap(bmp);
}
Problem solved:
public class MainActivity extends Activity {
ImageView img;
Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView1);
bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.lena);
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
{
Log.e("TEST", "Cannot connect to OpenCV Manager");
}
}
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Mat tmp = new Mat(bitmap.getWidth(), bitmap.getHeight(),CvType.CV_8UC1);
// Convert
Utils.bitmapToMat(bitmap, tmp);
Mat gray = new Mat(bitmap.getWidth(), bitmap.getHeight(),CvType.CV_8UC1);
// Conver the color
Imgproc.cvtColor(tmp, gray, Imgproc.COLOR_RGB2GRAY);
// Convert back to bitmap
Mat destination = new Mat(gray.rows(),gray.cols(),gray.type());
Imgproc.adaptiveThreshold(gray, destination, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);
Utils.matToBitmap(destination, bitmap);
img.setImageBitmap(bitmap);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
}

Android : Converting imageview to bitmap, to grayscale, bitmap to imageview

I don't don't get any warning on eclipse when I compile this code, but when I run it on device or emulator, that program was forced to close.
public class MainActivity extends Activity {
ImageView img;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//convert imageview to bitmap
img =(ImageView) findViewById(R.id.imageView1);
BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
final Bitmap imgbitmap = drawable.getBitmap();
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//convert bitmap to grayscale
Bitmap imgnew;
imgnew = toGrayscale(imgbitmap);
//convert bitmap to imageview
ImageView imgbit;
imgbit = (ImageView) findViewById(R.id.imageView2);
imgbit.setImageBitmap(imgnew);
}
});
}
public Bitmap toGrayscale(Bitmap bmpOriginal){
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
}
If above is your full code then, basic problem is there, you haven't define btn. You need to define it before using it, else when you ar egoing to click on the button it will not work. and this might closing your application.
btn=(Button) findViewById(R.id.button1);

flickering and erasing of the last image drawn in surfaceview of android

What I want to do is to draw an image when the touch is down and when it is moving the image should also move along with the finger and when the touch is up the image should remain in that position. Again if i put the finger down another copy of that same image should again appear in the touch down co-ordinates and the process continues.
I saw many tutorials and tried everything. I tried to save the image of the canvas when the finger is down and when it moves clear the screen with the image saved during the finger down but that did not work. Then, I tried to clear the canvas with black color and paste the image saved during finger down and then draw the new image that I want to appear but still it did not work properly. It causes flickering and sometimes when lift up my finger and then again put it down the last image gets erased.
This is my code:
public class renderView extends SurfaceView implements Runnable{
boolean isRunning=false;
SurfaceHolder holder;
Thread t=null;
Bitmap img;
Paint paint;
Canvas canvas2,canvas3;
Bitmap cache,backup,cache2;
float x,y;
boolean up,down,move;
boolean dummy;
public renderView(Context context) {
super(context);
isRunning=true;
holder = getHolder();
img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
paint= new Paint();
x=y=0;
cache = Bitmap.createBitmap(600, 800, Config.ARGB_8888);
cache2 = Bitmap.createBitmap(600, 800, Config.ARGB_8888);
backup = Bitmap.createBitmap(600,800, Bitmap.Config.ARGB_8888);
canvas2 = new Canvas(cache);
canvas3 =new Canvas(cache2);
move=down=false;
up=true;
dummy=false;
}
#Override
public void run() {
Log.d("status","run");
while(isRunning){
if(!holder.getSurface().isValid()){
continue;
}
Canvas canvas1 = holder.lockCanvas();
canvas1.drawColor(Color.BLUE);
canvas1.drawBitmap(cache2,0,0,paint);
canvas1.drawBitmap(cache,0,0,paint);
holder.unlockCanvasAndPost(canvas1);
}
}
public void pause(){
isRunning = false;
while(true){
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){
isRunning=true;
t=new Thread(this);
t.start();
}
public void down(float posX, float posY) {
/*x=posX;
y=posY;*/
canvas2.drawBitmap(img,posX,posY,paint);
canvas3.drawBitmap(img,posX,posY,paint);
up=false;
}
public void move(float posX, float posY) {
move=true;
dummy=true;
canvas2.drawColor(Color.BLACK);
canvas2.drawBitmap(cache2, 0, 0,paint);
canvas2.drawBitmap(img,posX,posY,paint);
}
public void up(float posX, float posY) {
up=true;
move=false;
canvas2.drawColor(Color.BLACK);
canvas2.drawBitmap(cache2,0,0,paint);
canvas2.drawBitmap(img,posX,posY,paint);
}
}

Categories

Resources