private void savePicture() {
mCamera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
String filePath = Environment.getExternalStorageDirectory()
.getPath() + "/";
String path = Environment.getExternalStorageDirectory()
.getPath() + "/Pictures/";
File pathDir = new File(path);
if (!pathDir.exists()) {
pathDir.mkdirs();
}
try {
Runtime.getRuntime().exec("screencap -p " + path + getCurSysDate() + ".png");
Log.e("路径", "screencap -p " + path + getCurSysDate() + ".png");
} catch (IOException e) {
e.printStackTrace();
}
final Bitmap mBitmap;
final Bitmap mBitmapText;
Matrix matrix = new Matrix();
mBitmap = Bitmap.createBitmap(bitmapResult, 0, 0, bitmapResult.getWidth(), bitmapResult.getHeight(), matrix, true);
mBitmapText = drawTextToLeftBottom(MainActivity.this, bitmapResult, "test123", 66, Color.RED, 0, 0);
saveBitmap(path + nowTime + ".jpg", mBitmapText);
}
});
}
public void saveBitmap(String filePath, Bitmap mBitmap) {
File f = new File(filePath);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "拍照成功!", Toast.LENGTH_LONG).show();
}
public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text, int size, int color, int paddingLeft, int paddingBottom) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
dp2px(context, paddingLeft),
bitmap.getHeight() - dp2px(context, paddingBottom));
}
public static int dp2px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text, Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
paint.setDither(true);
paint.setFilterBitmap(true);
if (bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, paddingLeft, paddingTop, paint);
return bitmap;
}
|