i have searched about how to draw prespctive image in canvas like in the picture but i dont find anything thing. if its not possible using canvas, how i can make something like it?
i find this:-
Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.img1);
Bitmap bitmap2=BitmapFactory.decodeResource(getResources(), R.drawable.img1);
Paint paint = new Paint();
paint.setAntiAlias(true);
Canvas canvas = new Canvas(resultingImage);
Path path=new Path();
path.moveTo(0,0);
float mm=1000;
path.lineTo(mm,0);
path.lineTo(mm, mm);
path.lineTo(0, mm);
path.lineTo(0,0);
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap2, 0, 0, paint);
img.setImageBitmap(resultingImage);
but this cut the image.
This is possible by magic method of canvas drawBitmapMesh.
This is example from my project:
static void Draw( Canvas canvas, Rect rt, Ini ini ){
final int sections = 256;
Point size = new Point();
float[] verts = Mesh.GetVertices( rt, sections, ini, size );
canvas.drawBitmapMesh( ini.bm, size.x, size.y, verts, 0, null, 0, null );
}
Related
I Convert Text to Bitmap with this method:
private Bitmap textToBitmap(String text)
{
Bitmap myBitmap = Bitmap.createBitmap(400, 50, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface clock = Typeface.createFromAsset(context.getAssets(),"fonts/myfont.ttf");
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(clock);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE );
paint.setTextSize(38);
paint.setTextAlign(Paint.Align.RIGHT);
myCanvas.drawText(text, 400, 30, paint);
return myBitmap;
}
But in some of phone my bitmap is very small and in other its very Big
why that Happen?
Bitmap myBitmap = Bitmap.createBitmap(400, 50, Bitmap.Config.ARGB_4444);//its unit is px, practical size of 1 px is different in various device.
You should resize bitmap according to device scaledDensity, here is a good solution
I've got a bitmap displayed in an ImageView, and I want to be able to make a certain percentage of the image black and white, and have the other part retain it's color. For example, if 60% is the target percentage, the image would look like this: . Thanks.
I've got a bitmap displayed in an ImageView, and I want to be able to
make a certain percentage of the image black and white, and have the
other part retain it's color. For example, if 60% is the target
percentage.
It seems (from your image) you mean monochrome (i.e. greyscale), not black and white.
Something like this should do it (tested o.k.):
void doIt(ImageView image)
{
//get bitmap from your ImageView (image)
Bitmap originalBitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
int height = originalBitmap.getHeight();
int fortyPercentHeight = (int) Math.floor(height * 40.0 / 100.0);
//create a bitmap of the top 40% of image height that we will make black and white
Bitmap croppedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth() , fortyPercentHeight );
//make it monochrome
Bitmap blackAndWhiteBitmap = monoChrome(croppedBitmap);
//copy the monochrome bmp (blackAndWhiteBitmap) to the original bmp (originalBitmap)
originalBitmap = overlay(originalBitmap, blackAndWhiteBitmap);
//set imageview to new bitmap
image.setImageBitmap(originalBitmap );
}
Bitmap monoChrome(Bitmap bitmap)
{
Bitmap bmpMonochrome = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpMonochrome);
ColorMatrix ma = new ColorMatrix();
ma.setSaturation(0);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(ma));
canvas.drawBitmap(bitmap, 0, 0, paint);
return bmpMonochrome;
}
Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
Bitmap bmp3 = bmp1.copy(Bitmap.Config.ARGB_8888,true);//mutable copy
Canvas canvas = new Canvas(bmp3 );
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmp3 ;
}
My app uses geojson to draw features on map. To draw feature:
for (GeoJsonFeature f : new GeoJsonLayer(googleMap, new JSONObject(geojson)).getFeatures())
{
f.setPointStyle(getPointStyle(text,status);
layer.addFeature(f);
}
layer.addLayerToMap();
To generate point (marker) style and set icon:
GeoJsonPointStyle markerStyle = new GeoJsonPointStyle();
int[] markers = {R.drawable.marker0,R.drawable.marker1,R.drawable.marker2,R.drawable.marker3,R.drawable.marker4};
bmp = Utils.drawTextToBitmap(getContext(), markers[status], icon);
markerStyle.setIcon(highlighted ? BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN):BitmapDescriptorFactory.fromBitmap(bmp));
drawTextToBitmap just generates bitmap with text on it:
public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText)
{
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null)
{
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setTextSize((int) (18 * scale));
Typeface tf = Typeface.createFromAsset(gContext.getAssets(), "fonts/fontawesome.ttf");
paint.setTypeface(Typeface.create(tf, Typeface.BOLD));
paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
Rect bounds = new Rect();
paint.getTextBounds(gText, 0, gText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width()) / 2;
int y = (bitmap.getHeight() + bounds.height()) / 2 -8;
canvas.drawText(gText, x, y, paint);
return bitmap;
}
My problem is that on some phones (not on every phone) this approach causes huge memory leak if there is big amount of markes to draw on map. Problem does not exists if I use static marker drawable from resources, but I need dynamic bitmap to add text on it.
Do you have any idea about memory leak cause? It seems map holds bitmap reference so it prevents garbage from disposing it. Even if map fragment is destroyed, map resources will be never released, so after displaying map few times, app stops working due to OutOfMemory exception.
I have a requirement, like drawing something in canvas and saving it to a larger image. As of now whatever I draw inside onDraw() method and save, it gives device provided image/canvas size, say something around 538(w)/852(h). I need image of almost double size, around 1000(w)/1500(h) without losing resolution. Any sample code, reference link would help definitely. Thanks in advance.
One way is to create Bitmap of desired size and draw on it using canvas:
int w = 1000, h = 1500;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Canvas canvas = new Canvas(bmp);
Finally you will have you bmp with your drawing and necessary dimentions.
EDIT::
#Override
protected void onDraw(Canvas canvas) {
int w = 1000, h = 1500;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Canvas mycanvas = new Canvas(bmp);
super.onDraw(mycanvas);
...
}
First get bitmap of your view using the function getDrawingCache(), then scale the bitmap as per your requirements.
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
//bitmap = bitmap.createScaledBitmap(..as your requirements..);
EDIT
You are saying that you want image in higher quality. You can't simply get a higher quality image from a lower quality image. However you can apply bitmap filtering to get slightly better quality.
Bitmap bitmap = getDrawingCache();
Bitmap output = Bitmap.createBitmap(/*width, height, bla bla bla*/);
Canvas canvas = new Canvas(output);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
Rect srcRect = new Rect(0,0,bitmap.getWidth(), bitmap.getHeight());
Rect destRect = new Rect(0,0,output.getWidth(), output.getHeight());
canvas.drawBitmap(bitmap, srcRect, destRect, paint);
I have the coordinates and I would like to draw a line or rectangular on a jpg image which is saved in my android device and then save the new file.
Is it possible? I am trying to us ImageIO but it isn't available in android or something goes wrong and it isn't acceptable?
Any idea or code?
This is how I was able to draw a green box on an existing JPEG.
Bitmap workingBitmap = BitmapFactory.decodeFile( mFullFilePath );
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
// bitmap needs to be mutable
Canvas tmpCanvas = new Canvas(mutableBitmap);
// setup paint parameters
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth( 5 );
paint.setColor(Color.GREEN);
// Rect object was passed; use below to test
// Rect rect = new Rect( 0, 0, 10, 10 );
tmpCanvas.drawRect(rect, paint);
// write the updated file out as a JPG
writeExternalToCache( mutableBitmap, mFullFilePath );
see also:
https://stackoverflow.com/a/13119762/3474698
https://stackoverflow.com/a/8801391/3474698