Android开发在图片上绘制文字

	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;
                //Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                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();
    }
	/* 绘制文字到左下方
     *
     * @param context
     * @param bitmap
     * @param text
     * @param size
     * @param color
     * @param paddingLeft
     * @param paddingBottom
     * @return
     */
    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));
    }

    /**
     * dip转pix
     *
     * @param context
     * @param dp
     * @return
     */
    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;
    }

猜你喜欢

转载自blog.csdn.net/qq_35761934/article/details/121794992