2011-09-06 98 views
16

我想在图像上绘制文本(用于保存带有文本的图像)。我有图像视图我将位图设置为该图像我想绘制图像上的文本(用户输入的文本)。我在保存前尝试这个.....如何在图片上绘制文字?

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代码..

<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> 
+0

到底什么是你的问题?您是否无法绘制文字或无法使用文字保存图像? – blessenm

+0

我的问题是我想在图像上添加文字。 –

回答

18

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

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(); 
    } 
} 

让我知道这是否适合你。

词shash

+0

它的工作谢谢ü非常.......后小的修改,我更新了答案。 –

+0

什么是originalBitmap? –

+0

我有这个解决方案的问题。显示以下错误:位图不可变。我如何解决这个问题? – LeandroPortnoy

0

充气在图像的文本视图。 有关基本示例,请参阅http://www.android10.org/index.php/forums/43-view-layout-a-resource/715-tutorial-android-xml-view-inflation。 这应该是最简单的方法。

LinearLayout lLayout; 

lLayout = (LinearLayout)findViewById(R.id.layout1); 

layout1 is the main layout. 

final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

TextView tv = (TextView)inflater.inflate(R.layout.text, null); 

lLayout.addView(tv); 
+0

在那只有按钮xml .. –

+0

将其替换为文本视图。在xml中有一个文本视图并按照他们为按钮完成的方式进行膨胀。 –

+0

放\t 在main.xml中 .............................然后复制并粘贴以上在你的oncreate一段代码。用main替​​换layout1。添加一个值在字符串s.xml –

1
  1. 创建一个空的位图
  2. 创建一个新的Canvas对象和该位图传递给它
  3. 呼叫view.draw(Canvas)的传递您刚才创建的画布对象。详情请参阅方法的文件。
  4. 使用Bitmap.compress()将位图的内容写入OutputStream文件中。

伪代码:

Bitmap bitmap = Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
canvas.drawText(); 
//necessary arguments and draw whatever you want. thes all are drawn on the bitmap.finally save this bitmap 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
+0

我必须使用这个..在保存功能或onCreate()。 –

+0

在保存在这个代码视图功能 –

+0

意味着.. –

1

您可以扩展视图中创建一个自定义视图。类似于

public class PieView extends View { 
    public PieView(Context context) { 
     super(context); 
     overlayBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.piechart_shade, 
     null); 
     overlayWidth = overlayBitmap.getWidth(); 
     setLayoutParams(new LayoutParams(overlayWidth, overlayWidth));  
    } 

    @Override  
    protected void onDraw(Canvas canvas) {  
     super.onDraw(canvas); 
    } 
} 

在ondraw方法中,您可以使用canvas.drawBitmap和canvas.drawText绘制位图和文本。

这样,您不需要framelayout,因为所有东西都在一个自定义视图中。

您可以在XML文件中包括此为

<com.raj.PieView android:id="@+id/framelayout" android:layout_marginTop="30dip"  
    android:layout_height="fill_parent" android:layout_width="fill_parent"/> 
+0

我有在相对布局和按钮的更多XML代码如何在我的代码添加这个.. –

44

由于suggested by Vladislav Skoumal,试试这个方法:

public Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) { 
    try { 
     Resources resources = mContext.getResources(); 
      float scale = resources.getDisplayMetrics().density; 
      Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId); 

      android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); 
      // set default bitmap config if none 
      if(bitmapConfig == null) { 
       bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; 
      } 
      // resource bitmaps are imutable, 
      // so we need to convert it to mutable one 
      bitmap = bitmap.copy(bitmapConfig, true); 

      Canvas canvas = new Canvas(bitmap); 
      // new antialised Paint 
      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
      // text color - #3D3D3D 
      paint.setColor(Color.rgb(110,110, 110)); 
      // text size in pixels 
      paint.setTextSize((int) (12 * scale)); 
      // text shadow 
      paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY); 

      // draw text to the Canvas center 
      Rect bounds = new Rect(); 
      paint.getTextBounds(mText, 0, mText.length(), bounds); 
      int x = (bitmap.getWidth() - bounds.width())/6; 
      int y = (bitmap.getHeight() + bounds.height())/5; 

      canvas.drawText(mText, x * scale, y * scale, paint); 

      return bitmap; 
    } catch (Exception e) { 
     // TODO: handle exception 



     return null; 
    } 

    } 

调用此方法

Bitmap bmp =drawTextToBitmap(this,R.drawable.aa,"Hello Android"); 

    img.setImageBitmap(bmp); 

了出来把enter image description here

+0

非常好,我的朋友! 你拯救我的一天! –

+1

救了我的也是:) –

+0

@Ruhollahツ我发布了这个答案4年前,现在有多个库可用于图像工作。 –

0

我解决这个问题(不可变的文件),但没有文件写入文件...请按照我的代码: public static File writeOnImage(File file)抛出IOException {

Bitmap originalBitmap = BitmapFactory.decodeFile(file.getPath()); 
    originalBitmap = convertToMutable(originalBitmap); 
    FileOutputStream out = new FileOutputStream(file); 

    try { 
     Canvas canvas = new Canvas(originalBitmap); 

     Paint paint = new Paint(); 
     paint.setColor(Color.BLACK); 
     paint.setStrokeWidth(12); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); 
     canvas.drawBitmap(originalBitmap, 0, 0, paint); 
     canvas.drawText("Testing...", 10, 10, paint); 

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

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

    return file; 
}