How to change buffered image and graphics in android? - java

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;
}

Related

Saving photo with transparent background to Contacts

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?

How to make images on Bitmap smaller?

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

Saving circle cropped image as JPEG format, background is not transparent in Android

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)
{
}
}

Webrtc rendering video screenshot

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.

JNA screenshot game

Need to make screenshot of some games. Found this JNA code, but when I try to do screen`s I just get black screen. When I try to do screen of some program, like WordPad ot smth it works well. As well I am bad in JNA, I want ask you about help. Is it possible to accomplish this task ?
public class Paint extends JFrame {
public BufferedImage capture(HWND hWnd) throws IOException {
String gettime = Gettime.screentime();
HDC hdcWindow = User32.INSTANCE.GetDC(hWnd);
HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow);
RECT bounds = new RECT();
User32Extra.INSTANCE.GetClientRect(hWnd, bounds);
int width = bounds.right - bounds.left;
int height = bounds.bottom - bounds.top;
HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height);
HANDLE hOld = GDI32.INSTANCE.SelectObject(hdcMemDC, hBitmap);
GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, hdcWindow, 0, 0, WinGDIExtra.SRCCOPY);
GDI32.INSTANCE.SelectObject(hdcMemDC, hOld);
GDI32.INSTANCE.DeleteDC(hdcMemDC);
BITMAPINFO bmi = new BITMAPINFO();
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
Memory buffer = new Memory(width * height * 4);
GDI32.INSTANCE.GetDIBits(hdcWindow, hBitmap, 0, height, buffer, bmi, WinGDI.DIB_RGB_COLORS);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, buffer.getIntArray(0, width * height), 0, width);
GDI32.INSTANCE.DeleteObject(hBitmap);
User32.INSTANCE.ReleaseDC(hWnd, hdcWindow);
File outputfile = new File("C:\\image" +gettime+ ".jpg");
ImageIO.write(image, "jpg", outputfile);
return image;
}
public static void main(String[] args) throws IOException {
new Paint();
}
BufferedImage image;
public Paint() throws IOException {
HWND hWnd = User32.INSTANCE.FindWindow(null, "some game");
this.image = capture(hWnd);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
}
#Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(image, 20, 40, null);
}
}
GDI32Util.getScreenshot(HWND hwnd)
Method is already provided in jna.
but my case is as the same as you.... the game screen is black... nothing...
Using JNA to take a screenshot sounds utterly complicated, besides not being platform-agnostic. Java has built-in functionality to take screenshots using the Robot class:
import java.awt.Robot;
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", new File("./screenshot.png"));
By adjusting the screenRect you could also just take a screenshot of a portion of the screen.

Categories

Resources