【问题标题】:Write Text over a image in android在android中的图像上写文本
【发布时间】:2014-12-24 12:05:39
【问题描述】:

在一个 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");
        }
    };

已编辑

我觉得我的问题不清楚,我想在我捕获的图像上添加水印。请帮助。

【问题讨论】:

标签: android image-capture


【解决方案1】:
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(); 
paint.setColor(Color.BLACK); 
paint.setTextSize(20); 
canvas.drawText("Some Text", 10, 25, paint); 

【讨论】:

    【解决方案2】:

    检查此代码: 首先在您设置图像的父布局中添加ImageViewTextView

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

    你来了!!你有带文字的图片。

    【讨论】:

      【解决方案3】:

      您可以使用此布局来显示图像:

      <?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>
      

      显示图像后,您可以设置任何您想要的文本。

      【讨论】:

      • 我认为您没有正确回答我的问题,基本上我需要一个带有一些文字的图像。当用户捕获图像时,sd 卡上捕获的图像应该有带有文字的图像。在捕获文本之前不应显示在表面视图上。
      猜你喜欢
      • 2016-09-29
      • 2011-10-13
      • 2011-11-07
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 2010-09-26
      相关资源
      最近更新 更多