how to create bitmap of scollview of images in android? - android

I’ve made a demo for film-strip in ,android. I am binding Images to my LinearLayout runtime, Now i want to save whole, Scrollable View to a bitmap, I have tried a lot but all gives solution to only save current visible screen to bitmap,can anyone please help me how to save whole scrollable View(Which is not visible to screen) in a bitmap, my code is as below:
xml
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="55dp"
android:scrollbars="none" >
<ScrollView
android:id="#+id/scroler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</HorizontalScrollView>
java
View insertPhoto(int i) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LayoutParams(252, 252));
layout.setGravity(Gravity.FILL);
final ImageView imageView = new ImageView(getApplicationContext());
imageView1 = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(250, 250));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageLoader.displayImage("file://" + dataT.get(i).sdcardPath,
imageView, new SimpleImageLoadingListener() {
public void onLoadingStarted(String imageUri, View view) {
imageView.setImageResource(R.drawable.no_media);
super.onLoadingStarted(imageUri, view);
}
});
layout.addView(imageView);
return layout;
}
using this I’ve made film-strip in vertical orientation. And now I want bitmap of this film-strip for save it.

public static Bitmap loadBitmapFromView(View v) {
Bitmap b = null;
v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
saveImageToInternalStorage(b);
return b;
}

Related

Create Bitmap From View in Android (with CardView)

In my app, I need to work with GoogleMap and on Markers, I need to put the next custom view
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="true"
android:layout_margin="1dp"
android:elevation="10dp"
app:cardCornerRadius="18dp">
<app.into.additional.DownloadImageView
android:id="#+id/offer_image_1"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp" />
</android.support.v7.widget.CardView>
DownloadImageView is a custom class that uses the Picasso framework to download some images.
The problem is the next one: if I add the above layout in a fragment, the ImageView is rounded as I need. When I try to create a Bitmap using the above layout, the Image loaded inside ImageView doesn't keep the round property, and it's shows square.
This is the code I'm using to create the Bitmap :
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
marker.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
marker.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
marker.draw(canvas);

How to programmatically take screenshot of an android application without notification and navigation bars

I am using the below code for taking screenshot in my application.
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.buildDrawingCache(true);
Bitmap bitmap = rootView.getDrawingCache();
But I am getting an image with two black portion in the top and bottom by the notification and navigation bars. I need to remove those black areas. I need not to replace those area with the notification and navigation bars. But i need the toolbar.
take the drawing cache of the root Layout as below:
//onCreate
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.activity_main);
layout.setDrawingCacheEnabled(true);
And to get bitmap, the usual way,
Bitmap bitmap = layout.getDrawingCache();
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary" />
<!--other content-->
</RelativeLayout>
Use Below Method:
public static Bitmap loadBitmapFromView(Context context, View v) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(returnedBitmap);
v.draw(c);
return returnedBitmap;
}
Pass your parent view inside the argument.
Take this to another Linear Layout
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:contentDescription="#string/app_name"
/>
</LinearLayout>
Now find Id for this Linear Layout on oncreate() method making it global variable
LinearLayout ll;
ll = (LinearLayout)findViewById(R.id.linear);
now capture this screen:
ll.setDrawingCacheEnabled(true);
ll.buildDrawingCache(true);
Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());
ll.setDrawingCacheEnabled(false);
Now set this bitmap to your ImageView.
Following is the XML layout file where I have added a button, textview and an imageview in a linear layout. And I have also added onClick attribute in the button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_screenshoat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ncrypted.myapplication.Screenshoat">
<Button
android:id="#+id/btnScreenshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Taake screen shot"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"/>
</LinearLayout>
This is android activity file where I have added some java code to take screenshot and import android.graphics.Bitmap;, java.io.ByteArrayOutputStream;, java.io.File; etc.
public class Screenshoat extends AppCompatActivity {
Button btnScreenshot;
Bitmap mbitmap;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screenshoat);
btnScreenshot = (Button) findViewById(R.id.btnScreenshot);
imageView = (ImageView) findViewById(R.id.imageView);
btnScreenshot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mbitmap = getBitmapOFRootView(v);
imageView.setImageBitmap(mbitmap);
createImage(mbitmap);
Toast.makeText(Screenshoat.this, "Screen shoat created", Toast.LENGTH_SHORT).show();
}
});
}
public Bitmap getBitmapOFRootView(View v) {
View rootview = v.getRootView();
rootview.setDrawingCacheEnabled(true);
Bitmap bitmap1 = rootview.getDrawingCache();
return bitmap1;
}
public void createImage(Bitmap bmp) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File file = new File(Environment.getExternalStorageDirectory() +
"/capturedscreenandroid.jpg");
try {
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(bytes.toByteArray());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Add Permission to read and write external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Why is my cardview not being drawn onto canvas?

I'm trying to draw a cardview onto a pdf its canvas. Unfortunately the cardview is not drawn. However some test text is drawn. Why is my carview not drawn?
Code:
// start a page
PdfDocument.Page page = document.startPage(pageInfo);
Canvas c = page.getCanvas();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1000, 50);
CardView card = new CardView(this);
card.setLayoutParams(params);
card.setRadius(4f);
Bitmap cardBitmap = Utils.loadBitmapFromView(card);
c.drawText("tessssssttt", 30, 30, p);
c.drawBitmap(cardBitmap, new Rect(0,0,1000,50), new Rect(5, 50, 995, 100), p);
document.finishPage(page);
View to bitmap:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
The following code draws CardView to bitmap.
But it's shadow dose not drawn.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = getLayoutInflater().inflate(R.layout.card_view, null, false);
Bitmap bitmap = loadBitmapFromView(view, 500, 500);
//This is sample code. So I print the bitmap to ImageView, not to PDF.
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
}
public static Bitmap loadBitmapFromView(View v, int width, int height) {
v.setMinimumWidth(width);
v.setMinimumHeight(height);
v.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
);
v.layout(0, 0, width, height);
v.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
card_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#33000000">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="This is sample."/>
</android.support.v7.widget.CardView>
</FrameLayout>

How can i capture my cameraview and my customview over camera simultaneously android

I am making an app in which i am opening a camera and above the camera i am drawing a flower picture from a custom view canvas object. Now i want to take picture what should i do to take picture of camera with that flower.
I have used this layout for that
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layoutCam">
<FrameLayout
android:id="#+id/preview"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
<ImageView
android:id="#+id/imgHole"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Take Snap"
android:id="#+id/btnTakeSnap"/>
</FrameLayout>
and java code is
public void onClick(View v)
{
preview.camera.takePicture(shutterCallback, rawCallback, postViewCallback, jpegCallback);
chal();
}
private boolean chal()
{
buttonTakeSnap.setVisibility(View.INVISIBLE);
View v1 = camView.getChildAt(1);
v1.setDrawingCacheEnabled(true);
v1.buildDrawingCache();
bmp = v1.getDrawingCache();
v1.setDrawingCacheEnabled(true);
buttonTakeSnap.setVisibility(View.VISIBLE);
Toast.makeText(this, "Akhtitaam", Toast.LENGTH_SHORT).show();
return true;
}
using this code i got two bitmaps one from chal(); method and second from jpegCallBack(); method now you can put these bitmaps on a same layout to overlap and capture that view
Using this code
Use this method to capture views
public static Bitmap loadBitmapFromView(View v)
{
Paint drawPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
drawPaint.setAntiAlias(false);
drawPaint.setFilterBitmap(false);
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawBitmap(b, 0, 0, drawPaint);
v.draw(c);
return b;
}
pass your camera's surface view as a parameter like this
View view=(YourParentLayout) findViewById(R.id.YourParentLayout);
loadBitmapFromView(view);

android frame layout with horizontal scroll bar and ovelapped images

i want to create a Frame layout to place a number of images from database, which need to be a horizontal scrollable list. Image Views are creating dynamically based on database value. Images must be overlapped like the attachment . Here is my xml
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_marginTop="20dp"
>
<FrameLayout
android:id="#+id/frmLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:layout_weight="1"
>
</FrameLayout>
</HorizontalScrollView>
and Here is java
public void getAllImages(){
cursorImages = dbAdapter.fetchImages();
if (cursorImages.moveToFirst()) {
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 2;
do {
String filename = cursorImages.getString(cursorImages
.getColumnIndex("filename"));
try {
InputStream ims = getAssets().open("images/" + filename);
Bitmap bm = BitmapFactory.decodeStream(ims,null,options);
Matrix mat = new Matrix();
mat.postRotate(30);
Bitmap bMapRotate = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mat, true);
ImageView im = new ImageView (this);
im.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
im.setImageBitmap(bMapRotate);
frmLayout.addView(im,new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
} catch (IOException ex) {
}
} while (cursorImages.moveToNext());
}
cursorImages.close();
}

Categories

Resources