I want to add a contact to the android contacts database which is working fine. As a Contact Photo there is a Vector from resource which gets converted to a Bitmap and then into a ByteArray like this:
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
drawable = (DrawableCompat.wrap(drawable)).mutate();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setARGB(255,255,255,255);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public static byte[] toByteArray(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
So basically its saved as PNG, so it should be transparent, right? For me its not. Does the Contact database support transparency or did I miss something at converting the Bitmap?
Related
I am trying to build a simple game on android and while setting the background image and other image assets on my main screen it is appearing too big as shown in the below image.
The actual size of the image is 1920x1080 and I want it to fit my screen like the image shown below:
The code for I have used is:
public class PoliceView extends View {
private Bitmap police;
private Bitmap background;
private Paint scorePaint = new Paint();
private Bitmap life[] = new Bitmap[2];
public PoliceView(Context context) {
super(context);
police = BitmapFactory.decodeResource(getResources(),R.drawable.police);
background = BitmapFactory.decodeResource(getResources(),R.drawable.gamebackground);
scorePaint.setColor(Color.BLACK);
scorePaint.setTextSize(70);
scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
scorePaint.setAntiAlias(true);
life[0] = BitmapFactory.decodeResource(getResources(),R.drawable.heart);
life[1] = BitmapFactory.decodeResource(getResources(),R.drawable.brokenheart);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//The order of draw matters as objects are drawn in this same order
canvas.drawBitmap(background,0,0,null);
canvas.drawBitmap(police, 0, 0, null);
canvas.drawText("Score:",20, 60, scorePaint);
//Three lives for the police
canvas.drawBitmap(life[0],580, 10, null);
canvas.drawBitmap(life[0],680, 10, null);
canvas.drawBitmap(life[0],780, 10, null);
}}
How can I resize the images to fit the screen??
i use this code for resize image
Bitmap tmp = BitmapFactory.decodeFile(mPath);
ResizeWidth = (int) (your size);
ResizeHeight = (int) (your size);
Bitmap bitmap = Bitmap.createBitmap(ResizeWidth, ResizeHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Bitmap scaled = Bitmap.createScaledBitmap(tmp, ResizeWidth, ResizeHeight, true);
int leftOffset = 0;
int topOffset = 0;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
How to resize bitmap in canvas?
canvas.save(); // Save current canvas params (not only scale)
canvas.scale(scaleX, scaleY);
canvas.restore(); // Restore current canvas params
scale = 1.0f will draw the same visible size bitmap
My requirement is, need to crop the image in circle shape and save as new image. For that, i have cropped the bitmap image using DrawRoundRect method of canvas in Android.
After cropped the image as circle and saving in PNG format, image background is transparent. But if i saved in JPEG format, image background was black. Please find my code
RoundedBitmap(Bitmap bitmap)
{
Bitmap roundBitmap = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(roundBitmap);
Paint paint = new Paint();
paint.AntiAlias = true;
RectF rectF = new RectF(0, 0, bitmap.Width, bitmap.Height);
canvas.DrawRoundRect(rectF, bitmap.Width / 2, bitmap.Height / 2, paint);
paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn));
canvas.DrawBitmap(bitmap, 0, 0, paint);
return roundedBitmap;
}
Using canvas.drawColor(Color.WHITE); not helped. I need transparent background color for JPEG format. Is it possible.?
Regards,
Bharathi.
PNG support transparent background while JPG doesn't . So you could create a white image, and then paint the original onto it.
public static void convertBitmapToJpg(Bitmap bitmap, string newImgpath)
{
Bitmap outB = bitmap.Copy(Bitmap.Config.Argb8888, true);
Canvas canvas = new Canvas(outB);
canvas.DrawColor(Color.White);
canvas.DrawBitmap(bitmap, 0, 0, null);
Java.IO.File file = new Java.IO.File(newImgpath);
try
{
MemoryStream outFile = new MemoryStream();
if (outB.Compress(Bitmap.CompressFormat.Jpeg, 100, outFile))
{
outFile.Flush();
outFile.Close();
}
}
catch (System.IO.FileNotFoundException e)
{
}
catch (System.IO.IOException e)
{
}
}
I want to take screenshot of rendered remotevideo view.
So far I tried these functions:
1.
public Bitmap getLastFrame(RelativeLayout remoteView)
{
Bitmap bitmap = null;
if(remoteView.getChildCount()>0) {
SurfaceViewRenderer rtcEAGLView = (SurfaceViewRenderer) remoteView.getChildAt(0);
rtcEAGLView.getBackground();
bitmap = ActivityUtils.takeScreenshot(rtcEAGLView);
}
return bitmap;
}
public static Bitmap takeScreenshot(View container) {
container.setVisibility(View.VISIBLE);
getScreenShot(container);
final Bitmap bmp = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bmp);
container.setDrawingCacheEnabled(true);
container.measure(
View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
container.layout(0, 0, container.getMeasuredWidth(), container.getMeasuredHeight());
canvas.drawBitmap(container.getDrawingCache(), 0, 0, new Paint());
return bmp;
}
void frame()
{
EGL10 egl = (EGL10) EGLContext.getEGL();
GL10 gl = (GL10)egl.eglGetCurrentContext().getGL();
Bitmap snapshotBitmap = createBitmapFromGLSurface(0, 0, 400, 400/* height*/, gl);
}
By trying these method I am not able to get screenshot as bitmap returns blank space only. rest of the area screenshot get captured but rendered part is not deplayed in that.
I'd like to create a Bitmap from a String with a given text size and set it as source of an ImageView.
The ImageView in it's layout xml:
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"/>
Setting the Bitmap as src of the ImageView:
myImageView.setImageBitmap(getBitmapFromString("StringToDraw", 30));
My getBitmapFromString method:
private Bitmap getBitmapFromString(String string, float textSize) {
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(textSize);
paint.setTextAlign(Paint.Align.LEFT);
canvas.drawText(string, 0, 100, paint);
return bitmap;
}
How can i calculate the proper size for the Bitmap (from the given text size and String length) and how can i make it to fit the ImageView properly?
Found the solution in the following answer to a similar question:
https://stackoverflow.com/a/15252876/3363481
My code looks like this now:
private Bitmap getBitmapFromString(String text, float fontSizeSP, Context context) {
int fontSizePX = convertDiptoPix(context, fontSizeSP);
int pad = (fontSizePX / 9);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setTextSize(fontSizePX);
int textWidth = (int) (paint.measureText(text) + pad * 2);
int height = (int) (fontSizePX / 0.75);
Bitmap bitmap = Bitmap.createBitmap(textWidth, height, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
float xOriginal = pad;
canvas.drawText(text, xOriginal, fontSizePX, paint);
return bitmap;
}
I would need to change the Java code to Android.
I'm having the Java code with BufferedImage and Graphics. How should I change the following Java code to Android.
My code is,
public static BufferedImage buff(BufferedImage bi){
if (isGray(bi)){
return bi;
}
BufferedImage gray = new BufferedImage(bi.getWidth(), bi.getHeight(), 10);
Graphics gr = gray.getGraphics();
gr.drawImage(bi, 0, 0, null);
gr.dispose();
return gray;
}
Thanks in advance.
javax.imageio isn't in the Android SDK, but the Bitmap class can be used, if you want convert to gray, you can use this:
public static Bitmap toGrayscale(Bitmap bm) {
Bitmap bmpGrayscale = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), 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(bm, 0, 0, paint);
return bmpGrayscale;
}