2011-09-06 121 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; 
}