2013-08-01 58 views
3

我想將Textiew添加到我的圖片並保存,但我不確定如何將圖片插入到圖片中。Android將文字添加到圖片並保存

我可以附加一張圖片在我的圖片保存它,它的工作原理,但現在我想插入Textiew到圖片中。

這裏是我的代碼:

PictureCallback cameraPictureCallbackJpeg = new PictureCallback() 
{ 
@Override 
public void onPictureTaken(byte[] data, Camera camera) 
{ 
    // TODO Auto-generated method stub 
    Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 

    wid = cameraBitmap.getWidth(); 
    hgt = cameraBitmap.getHeight(); 

    Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(newImage); 
    canvas.drawBitmap(cameraBitmap, 0f, 0f, null); 
    Drawable drawable = getResources().getDrawable(R.drawable.love); 
    drawable.setBounds(20, 20, 260, 160); 
    drawable.draw(canvas); 

    File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPicture/"); 
    storagePath.mkdirs(); 

    File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg"); 

    try 
    { 
    FileOutputStream out = new FileOutputStream(myImage); 
    newImage.compress(Bitmap.CompressFormat.JPEG, 80, out); 


    out.flush(); 
    out.close(); 
    } 
    catch(FileNotFoundException e) 
    { 
    Log.d("In Saving File", e + "");  
    } 
    catch(IOException e) 
    { 
    Log.d("In Saving File", e + ""); 
    } 

    camera.startPreview(); 

    drawable = null; 

    newImage.recycle(); 
    newImage = null; 

    cameraBitmap.recycle(); 
    cameraBitmap = null; 
} 
; 
}; 

回答

0

如果你只是想「文本」,而不一定是TextView,你可以直接在畫布上用drawText()繪製文本。

它只是更改爲類似:

Canvas canvas = new Canvas(newImage); 
    canvas.drawBitmap(cameraBitmap, 0f, 0f, null); 
    canvas.drawText("some text here", x, y, myPaint); 

    ... 

    newImage.compress(Bitmap.CompressFormat.JPEG, 80, out); 

如果你需要它是肯定的一個TextView,您可以將視圖轉換爲位圖,然後用drawBitmap()畫在畫布上。有關如何轉換它的示例,請參閱this answer

+0

我應該在myPaint中放置什麼? – alvin890101

+0

['Paint'](http://developer.android.com/reference/android/graphics/Paint.html)可以讓你定義文本的外觀。對齊方式,顏色,字體等。您可能只需傳遞'null',但我不知道默認的顏色是什麼樣的。我從來沒有用過它的文字。 – Geobits

相關問題