when I screen shot the map. I cannot success to shot the all screen.it only show the path.
I want to know what my problem is on my code. I hope someone can help me. thank you
It is my result:
// Screen shot
private static Bitmap takeScreenShot(Activity activity) {
// View to shot View
View view = activity.getWindow().getDecorView();
//View view = getPopupViews(getDecorViews())[0];
Log.i("ABC", view.getClass().getName());
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// the height
Rect frame = new Rect();
view.getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println(statusBarHeight);
// width and height
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
// del the state bar
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
// save image to sdcard
private static void savePic(Bitmap b, String strFileName) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos) {
b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void shoot() {
shoot(this);
}
// call function
public static void shoot(Activity a) {
savePic(takeScreenShot(a), "data/data/com.example.map/"+number+".png");
}
try this code, and pass mapView in this
public final static Bitmap takeScreenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
private Bitmap getMapImage() {
MapController mc = mapView.getController();
mc.setCenter(GEO_POINT);
mc.setZoom(ZOOM_LEVEL);
/* Capture drawing cache as bitmap */
mapView.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(mapView.getDrawingCache());
mapView.setDrawingCacheEnabled(false);
return bmp;
}
private void saveMapImage() {
String filename = "SCREEN_SHOT.png";
File f = new File(getExternalFilesDir(null), filename);
FileOutputStream out = new FileOutputStream(f);
Bitmap bmp = getMapImage();
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
}
Related
I use surface view for camera operation, After the image is captures am passing the image data to storeByteImage method.
But for certain devices or android lolipop version onwards am getting low resolution image.
In some new devices I get only 1/4 of the image captured.
public boolean storeByteImage(Context mContext, byte[] imageData, int quality) {
FileOutputStream fileOutputStream = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
_getdate.setDrawingCacheEnabled(true);
_getdate.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
_getdate.layout(0, 0, _getdate.getMeasuredWidth(),
_getdate.getMeasuredHeight()); // set the layout of the date in the image
_getdate.buildDrawingCache(true);
_getdate.buildDrawingCache();
try {
textimage = Bitmap.createBitmap(_getdate.getDrawingCache());
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
_getdate.setDrawingCacheEnabled(false); // clear drawing
// cache
Matrix matrix = new Matrix();
// the image height and width
int width, height;
height = myImage.getHeight();
width = myImage.getWidth();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(bitmap);
myImage = Bitmap.createScaledBitmap(myImage, width, height, true);
comboImage.drawBitmap(myImage, 0, 0, null);
comboImage.drawBitmap(textimage, matrix, null);
fileOutputStream = new FileOutputStream(
getResources().getString(R.string.sdpath_files_dir) + IMAGEFILE_EXT_JPG);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
If your activity has too much content and display a very long ui, like containing a ListView or WebView,
How can we get a screenshot containing the whole content within a long image?
I think the answer in this link can help you:
How to convert all content in a scrollview to a bitmap?
I mixed two answers in the link above to make an optimized piece of code for you:
private void takeScreenShot()
{
View u = ((Activity) mContext).findViewById(R.id.scroll);
HorizontalScrollView z = (HorizontalScrollView) ((Activity) mContext).findViewById(R.id.scroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
Bitmap b = getBitmapFromView(u,totalHeight,totalWidth);
//Save bitmap
String extr = Environment.getExternalStorageDirectory()+"/Folder/";
String fileName = "report.jpg";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(mContext.getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
int height = Math.min(MAX_HEIGHT, totalHeight);
float percent = height / (float)totalHeight;
Bitmap canvasBitmap = Bitmap.createBitmap((int)(totalWidth*percent),(int)(totalHeight*percent), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(canvasBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
canvas.save();
canvas.scale(percent, percent);
view.draw(canvas);
canvas.restore();
return canvasBitmap;
}
Try using this,
Pass your view to this function,
public Bitmap getBitmapFromView(View view) {
// Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
// Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
// Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
// has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
// does not have background drawable, then draw white background on
// the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
// return the bitmap
return returnedBitmap;
}
This function will return a bitmap, you can utilize it the way you want.
You can easily convert layout into "bitmap image"
protected Bitmap ConvertToBitmap(LinearLayout layout) {
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap bitmap = layout.getDrawingCache();
return bitmap;
}
I'd like to convert a dynamic string to Bitmap and save it on disk. The image should appear like the image below with the corner around the string.
I couldnt find a way to direct convert a String to bitmap, I tried to create a textView and convert it to bitmap, but it dont work properly.
Obs: I saw some questions that is similar, but they really don't have a solution to this question.
public static String StringToBitMap(Context context, String name){
TextView textView = new TextView(context);
textView.setText(name);
try{
int w = textView.getWidth();
int h = textView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(w, h,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
Drawable bgDrawable = textView.getBackground();
if (bgDrawable!=null) {
bgDrawable.draw(canvas);
}
else {
canvas.drawColor(Color.WHITE);
}
textView.draw(canvas);
FileOutputStream out = null;
File file = null;
try {
String path = Environment.getExternalStorageDirectory().toString();
file = new File(path + "/Download/", "item.png");
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
out.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return file.getAbsolutePath();
}catch(Exception e){
e.getMessage();
return null;
}
}
I just found the solution:
public static String StringToBitMap(Context context, String name){
try{
// Create bitmap
Bitmap bitmap = Bitmap.createBitmap(250, 16,Bitmap.Config.ARGB_8888);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(0xffffffff);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.BLACK);
// text size in pixels
paint.setTextSize(12);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(name, 0, name.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/2;
int y = (bitmap.getHeight() + bounds.height())/2;
canvas.drawText(name, x, y, paint);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
// draw corner to the Canvas
canvas.drawRect(x - 5, 0.0f, x + bounds.width() + 5, 16.0f, paint);
// save on disk
FileOutputStream out = null;
File file = null;
try {
String path = Environment.getExternalStorageDirectory().toString();
file = new File(path + "/Download/", "item.png");
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
out.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return file.getAbsolutePath();
}catch(Exception e){
e.getMessage();
return null;
}
}
I am trying to compress a bitmap, but getting a null pointer exception..the logcat is displaying uncaught exception
Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes , 0, imageAsBytes .length);
Bitmap bPNGcompress =codec(bitmap, Bitmap.CompressFormat.PNG, 0);
Bitmap scaled = bPNGcompress.createScaledBitmap( bPNGcompress, 100, 100, true );
method implementation
private static Bitmap codec(Bitmap map, Bitmap.CompressFormat format,
int quality) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
map.compress(format, quality, os);
try {
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] array = os.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
Please try this,
private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,int quality)
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
src.compress(format, quality, os);
byte[] array = os.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
#Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawColor(Color.GRAY);
// you need to insert some image flower_blue into res/drawable folder
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
// Best of quality is 80 and more, 3 is very low quality of image
Bitmap bJPGcompress = codec(b, Bitmap.CompressFormat.JPEG, 3);
// get dimension of bitmap getHeight() getWidth()
int h = b.getHeight();
canvas.drawBitmap(b, 10,10, paint);
canvas.drawBitmap(bJPGcompress, 10,10 + h + 10, paint);
}
}
I read a lot of topics where bitmap was resizes by decodeFile and createScaledBitmap. But I would like to change this file, without create extra bitmaps or files. It means, just when I open this file bitmap will be smaller/bigger. Is it possibility?
Edit:
Specifically, I have jpeg files and I will zip this files, and before making zip I set size of images (in zip file).
I'm not clearly understood, what you want to obtain,
but here is a simple code to resize image file
String your_file_path = "image.png";
int set_scale_your_need = 2;
//getting your image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(your_file_path, o);
o.inJustDecodeBounds = false;
o.inSampleSize = set_scale_your_need;
Bitmap bitmap = BitmapFactory.decodeFile(your_file_path, o);
FileOutputStream fos = new FileOutputStream(your_file_path, false);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
If you just want to have it open as a smaller version of it's self you can use BitmapFactory.Options.inSampleSize to reduce the load size. If you want to actually resize the image and resave it to the file system, you will want to do that in an AsyncTask,
something like this:
public class ImageResizerTask extends AsyncTask<Integer, Void, Bitmap> {
Bitmap mBitmap;
String filePath;
Context mContext;
Activity mCallBack;
//ProgressDialog pd;
public ImageResizerTask(Context context, String path,
Activity callBack) {
mContext = context;
filePath = path;
mBitmap = BitmapFactory.decodeFile(filePath);
mCallBack = callBack;
}
#Override
protected void onPreExecute() {
}
#Override
protected Bitmap doInBackground(Integer... params) {
// TODO Auto-generated method stub
resize(mBitmap);
return mBitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
//pd.dismiss();
bitmap.recycle();
mCallBack.onImageResized(filePath);
}
private void resize(Bitmap tmp) {
final Bitmap bitmap = Bitmap.createBitmap(500, 500,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
Log.v("TMPBMP",
"temp bmp width:" + tmp.getWidth() + " height:"
+ tmp.getHeight());
if (tmp.getWidth() < tmp.getHeight()) {
final int width = (int) (1f * tmp.getWidth() / tmp.getHeight() * 500);
final int height = 500;
final Bitmap scaled = Bitmap.createScaledBitmap(tmp, width, height,
false);
final int leftOffset = (bitmap.getWidth() - scaled.getWidth()) / 2;
final int topOffset = 0;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
} else {
final int width = 500;
final int height = (int) (1f * tmp.getHeight() / tmp.getWidth() * 500);
;
final Bitmap scaled = Bitmap.createScaledBitmap(tmp, width, height,
false);
final int leftOffset = 0;
final int topOffset = (bitmap.getHeight() - scaled.getHeight()) / 2;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
}
FileOutputStream outStream;
try {
outStream = new FileOutputStream(filePath);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}