Webrtc rendering video screenshot - java

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.

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

I want bitmap Left and right side edges to be blurred out

I have seen many answers on stackoverflow and tried many possible combinations but still i can't get the expected bllureed image as shown below:
Side Blur Edges
The code i tried (using canvas and then overlaying bot images)
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth());
int height = Math.round(image.getHeight());
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(25);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
public static Bitmap overlayBitmap(Bitmap bitmapBackground, Bitmap bitmapImage) {
Bitmap resized_bitmap = Bitmap.createScaledBitmap(bitmapImage, (int) (bitmapBackground.getWidth()*0.8), bitmapBackground.getWidth(),true);
Bitmap bmOverlay = Bitmap.createBitmap(bitmapBackground.getWidth(), bitmapBackground.getHeight(), bitmapBackground.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bitmapBackground, new Matrix(), null);
canvas.drawBitmap(resized_bitmap,bitmapBackground.getWidth()/5,bitmapBackground.getHeight()/5, null);
return bmOverlay;
}
Not getting the desired blur using this code tried several combinations but still no success.
When Trying to blur out both sides of my ImageView I used the following code:
private void applyBlur(final ImageView image) {
image.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
image.getViewTreeObserver().removeOnPreDrawListener(this);
image.buildDrawingCache();
Bitmap bmp = image.getDrawingCache();
blur(bmp, image);
return true;
}
});
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void blur(Bitmap mBitmap, View view) {
long startMs = System.currentTimeMillis();
float radius = 20;
Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()),
(int) (view.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.translate(-view.getLeft(), -view.getTop());
canvas.drawBitmap(mBitmap, 100, 0,null);
canvas.drawBitmap(mBitmap, -100, 0,null);
RenderScript rs = RenderScript.create(getApplicationContext());
Allocation overlayAlloc = Allocation.createFromBitmap(
rs, overlay);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(radius);
blur.forEach(overlayAlloc);
overlayAlloc.copyTo(overlay);
view.setBackground(new BitmapDrawable(
getResources(), overlay));
rs.destroy();
}//end blur
The key to blurring out both sides of the ImageView instead of just the left side
changing the 2nd parameter in the canvas.drawBitmap to a negative value
eg 100 becomes -100
so after canvas.drawBitmap(mBitmap, 100, 0,null); you add canvas.drawBitmap(mBitmap, -100, 0,null);
Hope this helps.

How to change buffered image and graphics in android?

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

how return picture after after redraw with Bitmap

Help me realized some logic.
I have a picture which redrawing by:
public void onClick(View v) {
BitmapDrawable mydrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap b = mydrawable.getBitmap();
b = doHighlightImage(b);
imageView.setImageBitmap(b);
}
});
public static Bitmap doHighlightImage(Bitmap src) {
// создадим новый битмап, который станет итоговым
Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96,
src.getHeight() + 96, Bitmap.Config.ARGB_8888);
// подключаем холст
Canvas canvas = new Canvas(bmOut);
// установим цвет по умолчанию
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
// создадим размытие для прозрачности
Paint ptBlur = new Paint();
ptBlur.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
int[] offsetXY = new int[2];
// получим прозрачный слепок из изображения
Bitmap bmAlpha = src.extractAlpha(ptBlur, offsetXY);
// готовимся к рисованию
Paint ptAlphaColor = new Paint();
ptAlphaColor.setColor(0xFFFFFFFF);
// закрашиваем цветом цветной слепок (bitmap)
canvas.drawBitmap(bmAlpha, offsetXY[0], offsetXY[1], ptAlphaColor);
// освобождаем ресурсы
bmAlpha.recycle();
// рисуем исходник
canvas.drawBitmap(src, 0, 0, null);
// возвращаем финальный рисунок
return bmOut;
}
But, after redrawing i must return the initial state of the picture. How to do this?
What do you mean by must return the initial state of the picture? Bitmap is immutable, so just keep a reference to whatever bitmap you need and don't worry about changing it - it's impossible.

Categories

Resources