[英]Write Text over a image in android
在一個Android應用程序中,我具有使用自定義相機視圖的照片捕獲功能。我正在使用SurfaceHolder.Callback並能夠捕獲圖像。但是現在我想在捕獲的圖像上顯示一些文本。 這個怎么做。 下面是我的代碼-
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
和回調方法-
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
long time = 0;
try {
new File(filePath).createNewFile();
outStream = new FileOutputStream(filePath);
outStream.write(data);
captureBtn.setVisibility(View.GONE);
preview.shutdownCamera();
preview.setVisibility(View.GONE);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
EDITED
我想我的問題不清楚,我想在捕獲的圖像上添加水印。請提供任何幫助。
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
檢查此代碼:首先在父布局中添加ImageView
和TextView
,在其中設置像這樣的圖像。
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(result.getWidth(), result.getHeight());
imgPreview.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(result.getWidth(), result.getHeight());
txtPreview.setLayoutParams(params);
然后將其添加到這樣的父布局中( rlPreview
是父布局):
txtPreview.setText("Enter text which you want to display in image");
txtPreview.setTextSize(5.0f);
rlPreview.addView(imgPreview);
rlPreview.addView(txtPreview);
然后,執行以下操作以創建帶有文本的圖像:
rlPreview.setDrawingCacheEnabled(true);
Bitmap bitmapWithWaterMark = Bitmap.createScaledBitmap( rlPreview.getDrawingCache(), rlPreview.getWidth(), rlPreview.getHeight(), true);
rlPreview.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Bitmap bmp = bitmapWithWaterMark.compress(Bitmap.CompressFormat.PNG, 0, bytes);
你去! 您有帶文字的圖像。
您可以使用以下布局顯示圖像:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
顯示圖像后,您可以設置所需的任何文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.