android 圖片加文字
阿新 • • 發佈:2019-02-15
兩種方法:
1.直接在圖片上寫文字
String str = "PICC要寫的文字";
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
int width = photo.getWidth(), hight = photo.getHeight();
System.out.println("寬"+width+"高"+hight);
icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一個空的BItMap
Canvas canvas = new Canvas(icon);//初始化畫布繪製的影象到icon上
Paint photoPaint = new Paint(); //建立畫筆
photoPaint.setDither(true); //獲取跟清晰的影象取樣
photoPaint.setFilterBitmap(true);//過濾一些
Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//建立一個指定的新矩形的座標
Rect dst = new Rect(0, 0, width, hight);//建立一個指定的新矩形的座標
canvas.drawBitmap(photo, src, dst, photoPaint);//將photo 縮放或則擴大到 dst使用的填充區photoPaint
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//設定畫筆
textPaint.setTextSize(20.0f);//字型大小
textPaint.setTypeface(Typeface.DEFAULT_BOLD);//採用預設的寬度
textPaint.setColor(Color.RED);//採用的顏色
//textPaint.setShadowLayer(3f, 1, 1,this.getResources().getColor(android.R.color.background_dark));//影音的設定
canvas.drawText(str, 20, 26, textPaint);//繪製上去字,開始未知x,y採用那隻筆繪製
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
image.setImageBitmap(icon);
saveMyBitmap(icon);
2.將兩個圖片合成
onCreat方法裡面{
Bitmap mark = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
Bitmap a = createBitmap(photo,mark);
image.setImageBitmap(a);
saveMyBitmap(a);
}
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
// Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//建立一個新的和SRC長度寬度一樣的點陣圖
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0座標開始畫入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角畫入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//儲存
//store
cv.restore();//儲存
return newb;
}
//儲存圖片到data下面
public void saveMyBitmap(Bitmap bmp){
FileOutputStream fos = null;
try {
fos = openFileOutput("image1.jpg", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
}
}
}
}
1.直接在圖片上寫文字
String str = "PICC要寫的文字";
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
int width = photo.getWidth(), hight = photo.getHeight();
System.out.println("寬"+width+"高"+hight);
icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一個空的BItMap
Canvas canvas = new Canvas(icon);//初始化畫布繪製的影象到icon上
Paint photoPaint = new Paint(); //建立畫筆
photoPaint.setDither(true); //獲取跟清晰的影象取樣
photoPaint.setFilterBitmap(true);//過濾一些
Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//建立一個指定的新矩形的座標
Rect dst = new Rect(0, 0, width, hight);//建立一個指定的新矩形的座標
canvas.drawBitmap(photo, src, dst, photoPaint);//將photo 縮放或則擴大到 dst使用的填充區photoPaint
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//設定畫筆
textPaint.setTextSize(20.0f);//字型大小
textPaint.setTypeface(Typeface.DEFAULT_BOLD);//採用預設的寬度
textPaint.setColor(Color.RED);//採用的顏色
//textPaint.setShadowLayer(3f, 1, 1,this.getResources().getColor(android.R.color.background_dark));//影音的設定
canvas.drawText(str, 20, 26, textPaint);//繪製上去字,開始未知x,y採用那隻筆繪製
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
image.setImageBitmap(icon);
saveMyBitmap(icon);
2.將兩個圖片合成
onCreat方法裡面{
Bitmap mark = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
Bitmap a = createBitmap(photo,mark);
image.setImageBitmap(a);
saveMyBitmap(a);
}
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
// Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//建立一個新的和SRC長度寬度一樣的點陣圖
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0座標開始畫入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角畫入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//儲存
//store
cv.restore();//儲存
return newb;
}
//儲存圖片到data下面
public void saveMyBitmap(Bitmap bmp){
FileOutputStream fos = null;
try {
fos = openFileOutput("image1.jpg", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
}
}
}
}