如何绘制文本上的形象?文本、形象

2023-09-12 00:29:45 作者:い男人不狠灬

我想绘制文本图像(用于保存文本的图像)。我有图像视图我设置位图的形象我想提请对图像中的文字(文字由用户输入)。在保存前我想这.....

 无效saveImage(){
    文件MYDIR =新的文件(/ SD卡/ saved_images);
    myDir.mkdirs();
    随机数发生器=新的随机();
    INT N = 10000;
    N = generator.nextInt(N);
    字符串FNAME =图像 - + N +JPG;
    档案文件=新的文件(MYDIR,FNAME);
    如果(file.exists())file.delete();
    尝试 {
           FileOutputStream中出=新的FileOutputStream(文件);
           originalBitmap.com preSS(Bitmap.Com pressFormat.JPEG,90,出);
           了out.flush();
           out.close();

    }赶上(例外五){
           e.printStackTrace();
    }
}
 

XML code是..

 <的FrameLayout
     机器人:ID =@ + ID /的FrameLayout
     机器人:layout_marginTop =30dip
     机器人:layout_height =FILL_PARENT
     机器人:layout_width =FILL_PARENT>

     < ImageView的
          机器人:ID =@ + ID / ImageView01
          机器人:layout_alignParentTop =真
          机器人:layout_height =WRAP_CONTENT
          机器人:layout_width =WRAP_CONTENT/>

     < TextView的机器人:ID =@ + ID / text_view2
          机器人:layout_marginTop =20dip
          机器人:layout_width =WRAP_CONTENT
          机器人:文本=SampleText
          机器人:TEXTSIZE =12磅
          机器人:layout_alignTop =@ + ID / ImageView01
          机器人:layout_height =WRAP_CONTENT/>

< /的FrameLayout>
 

解决方案

更​​新SaveImage()方法,以支持文本绘制。

 无效saveImage(){
    文件MYDIR =新的文件(/ SD卡/ saved_images);
    myDir.mkdirs();
    随机数发生器=新的随机();
    INT N = 10000;
    N = generator.nextInt(N);
    字符串FNAME =图像 - + N +JPG;
    档案文件=新的文件(MYDIR,FNAME);
    如果(file.exists())file.delete();
    尝试 {
        FileOutputStream中出=新的FileOutputStream(文件);

        //新增加的code从这里开始[
            帆布油画=新的Canvas(originalBitmap);

            涂料粉刷=新的油漆();
            paint.setColor(Color.WHITE); //文字颜色
            paint.setStrokeWidth(12); //文字大小
            paint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); //文字重叠式
            //一些设置...

            canvas.drawBitmap(originalBitmap,0,0,油漆);
            canvas.drawText(测试...,10,10,漆);
        //新增加的code到此为止]

        originalBitmap.com preSS(Bitmap.Com pressFormat.JPEG,90,出);
        了out.flush();
        out.close();
    }赶上(例外五){
       e.printStackTrace();
    }
}
 
EPS形象图模板 EPS格式形象图模板素材图片 EPS形象图模板设计模板 我图网

让我知道这对你的作品。

词shash

I want to draw text on image ( for saving that image with text ). i have image view i set bitmap to that image i want to Draw the text on image (text entered by user ). i tried this before saving.....

void saveImage() {
    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";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

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

Xml code is..

<FrameLayout 
     android:id="@+id/framelayout"
     android:layout_marginTop="30dip"
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent">

     <ImageView 
          android:id="@+id/ImageView01"
          android:layout_alignParentTop="true"
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content"/>

     <TextView android:id="@+id/text_view2"
          android:layout_marginTop="20dip"
          android:layout_width="wrap_content" 
          android:text="SampleText"
          android:textSize="12pt"
          android:layout_alignTop="@+id/ImageView01" 
          android:layout_height="wrap_content"/>  

</FrameLayout>

解决方案

Updated SaveImage() method, to support text drawing.

void saveImage() {
    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";
    File file = new File (myDir, fname);
    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.WHITE); // 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...", 10, 10, paint);
        // NEWLY ADDED CODE ENDS HERE ]

        originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
       e.printStackTrace();
    }
}

Let me know if this works for you.

Shash