如何在捕获的图像上绘制文本?

时间:2018-04-10 10:38:58

标签: android android-imageview

您好我正在尝试使用下面的代码在捕获的图像上添加文本,然后我想在设备内部存储中使用单独的文件夹保存该文本绘图图像。

但是使用我的下面的代码,该文件夹没有显示在内部存储中。

有人可以帮助我,我做错了什么?

代码: -

 /**
     * onCaptureImageResult
     *
     * @param
     */
    private void onCaptureImageResult() {

        try {
            File shopImageFile = new File(Utilities.getRealPathFromURI(imageUri, this));
            Bitmap thumbnail = Bitmap.createScaledBitmap(BitmapFactory.decodeFile("" + shopImageFile),
                    100, 100, true);
            saveImage(thumbnail);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }


    /**
     * saveImage
     */
    private void saveImage(Bitmap originalBitmap) {

        //File myDir = new File("/sdcard/saved_images");
        //myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        // String fname = "Image-" + n + ".jpg";
        ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
        //File file = new File(myDir, fname);
        File file = wrapper.getDir("Images", MODE_PRIVATE);

        if (file.exists()){
            file.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(file);

            // NEWLY ADDED CODE STARTS HERE [
            Canvas canvas = new Canvas(originalBitmap);
            Paint paint = new Paint();
            paint.setColor(Color.RED); // Text Color
            paint.setStrokeWidth(12); // Text Size
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
            // some more settings...

            canvas.drawBitmap(originalBitmap, 0, 0, paint);
            canvas.drawText("Testing...", 100, 100, paint);
            // NEWLY ADDED CODE ENDS HERE ]

            originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

亲爱的用这个library在捕获的图像上绘制文本。

compile('com.tengio.android:graffiti:latest_version')

打开这样的图片

Uri uri = new Uri.Builder()
                .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
                .path(image path)
                .build();