Friday, 17 May 2013

Saving a nice screenshot from a Sony SmartWatch for use in promotional listing graphics

I hate fiddling with promo graphics, screen shots and stuff particularly with the Sony SmartWatch because you ideally want to put a border around the screenshot. That should look something like this from our game play.google.com/store/oneuppoker:
 
It's a pain to do and at some point you'll have to do it all over again because you changed the look of your game/app. That was till now!

This post assumes you can get to a 128x128 bitmap no problem and pass it into the "screen" parameter. This one method will do all the heavy lifting to creating a beautiful png right into your DCIM folder:

public void saveScreenShot(final Bitmap screen, final boolean transparent) {
  Options o = new Options();
  o.inDensity = DisplayMetrics.DENSITY_DEFAULT;
  o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;

  Bitmap b = BitmapFactory.decodeResource(getResources(),
    R.drawable.smartwatch_face_image_transparent, o);

  Bitmap target = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
    b.getConfig());
  target.setDensity(b.getDensity());

  Canvas c = new Canvas(target);

  if (!transparent) {
   // optional white background
   c.drawColor(0xffffffff);
  }

  Matrix m = new Matrix();
  m.setTranslate(64, 142);
  final float scale = 2.44f / 1.28f;
  m.preScale(scale, scale);

  Paint white = new Paint();
  white.setARGB(255, 255, 255, 255);

  //draw the image
  c.drawBitmap(screen, m, white);

  // draw the overlay
  c.drawBitmap(b, 0, 0, new Paint());

  //save it
  File dcimDir = Environment
    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
  File picsDir = new File(dcimDir, "SmartWatchScreenShots"); // folder name under DCIM
  picsDir.mkdirs();
  File newFile = getNextPhotoFile(picsDir);
  OutputStream os;
  try {
   os = new FileOutputStream(newFile);

   target.compress(CompressFormat.PNG, 100, os);

   os.flush();
   os.close();
   b.recycle();
   target.recycle();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
The painstakingly prepared resource file mentioned is this, right click and save as into your res\drawable dir.
 
 Note: read this stackoverflow.com/android-how-to-use-mediascannerconnection-scanfile if you want your device to show the new screenshot in it's photo library.

No comments:

Post a Comment