Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 合并两个重叠的图像_Java_Android_Image_Overlay_Android Canvas - Fatal编程技术网

Java 合并两个重叠的图像

Java 合并两个重叠的图像,java,android,image,overlay,android-canvas,Java,Android,Image,Overlay,Android Canvas,您好,我有一个双图像视图,一个是从相机中选择的图片,另一个图像视图只有“Make Hawk Nelson”这样的文本。双图像视图的图像如下 xml代码如下所示 <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="7" android:scaleType="fitXY" > <Ima

您好,我有一个双图像视图,一个是从相机中选择的图片,另一个图像视图只有“Make Hawk Nelson”这样的文本。双图像视图的图像如下

xml代码如下所示

  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="7"
    android:scaleType="fitXY" >

    <ImageView
        android:id="@+id/imgSelectedPhoto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/txt_made_hawk_nelson"
        android:layout_centerInParent="true" />

</RelativeLayout>

上面是半屏幕代码,上面的图像也是半屏幕


现在我想保存这张照片,有人能帮我吗?我该怎么做?可能帆布会帮助我,但我不知道怎么做,所以请任何人都能帮助我

你有正确的想法。画布是最简单的方法

但您必须首先了解,这些图像视图只是屏幕上的表示,使用这两个图像创建位图与屏幕上的表示关系不大

与图像的内存表示一样,您将拥有可绘图(根据屏幕大小预先缩放,以便根据ldpi、mdpi、hdpi和xhdpi文件夹的不同设备的大小不同)和位图,它们是绝对表示

我刚才所说的将根据您的应用程序而有所不同,我不会给出确切的解决方案,但会向您解释所有的概念:

举个例子,假设背景和文本都是位图对象,那么代码是:

// Init our overlay bitmap
Bitmap bmp = backgroundBitmap.copy(Bitmap.Config.ARGB_8888, true);
// Init the canvas
Canvas canvas = new Canvas(bmp);
// Draw the text on top of the canvas
canvas.drawBitmap(textBitmap, 0, 0, null);

// now bmp have the two overlayed:
您可以(也应该)进行一些计算,并使用
drawBitmap()
方法中的值0和0将文本置于画布的中心

或者,如果您有可绘制的(例如getResources.getDrawable(R.drawable.bkgr);),则可以使用绘制到画布,并使用getIntrinsicHeight和getIntrinsicWidth使用创建位图


快乐编码

你的想法是对的。画布是最简单的方法

但您必须首先了解,这些图像视图只是屏幕上的表示,使用这两个图像创建位图与屏幕上的表示关系不大

与图像的内存表示一样,您将拥有可绘图(根据屏幕大小预先缩放,以便根据ldpi、mdpi、hdpi和xhdpi文件夹的不同设备的大小不同)和位图,它们是绝对表示

我刚才所说的将根据您的应用程序而有所不同,我不会给出确切的解决方案,但会向您解释所有的概念:

举个例子,假设背景和文本都是位图对象,那么代码是:

// Init our overlay bitmap
Bitmap bmp = backgroundBitmap.copy(Bitmap.Config.ARGB_8888, true);
// Init the canvas
Canvas canvas = new Canvas(bmp);
// Draw the text on top of the canvas
canvas.drawBitmap(textBitmap, 0, 0, null);

// now bmp have the two overlayed:
您可以(也应该)进行一些计算,并使用
drawBitmap()
方法中的值0和0将文本置于画布的中心

或者,如果您有可绘制的(例如getResources.getDrawable(R.drawable.bkgr);),则可以使用绘制到画布,并使用getIntrinsicHeight和getIntrinsicWidth使用创建位图

快乐编码

根据需要更改布局

<Framelayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="7" >

<ImageView
    android:id="@+id/imgSelectedPhoto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY" />

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/txt_made_hawk_nelson"
    android:layout_centerInParent="true" />
将布局更改为

<Framelayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="7" >

<ImageView
    android:id="@+id/imgSelectedPhoto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY" />

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/txt_made_hawk_nelson"
    android:layout_centerInParent="true" />

这可能会帮助您,这里需要使用所需字符串更改函数,并传递您的背景<代码>可绘制的图像, 并使用
画布
调整字体大小、文本颜色和对齐方式,创建一个
位图
并进行检查

private Bitmap getThumb(String strangle, String strnote, int width, int height) {

  //Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.transperet_bg);
  Bitmap bmOverlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

  Canvas canvas = new Canvas(bmOverlay);
  Paint paint = new Paint();

  paint.setColor(Color.WHITE);
  paint.setTextSize(20);
  paint.setFlags(Paint.ANTI_ALIAS_FLAG);
  // if the background image is defined in main.xml, omit this line
  canvas.drawARGB(140, 0, 0, 0);
  //canvas.drawBitmap(mBitmap, 0, 0, null);
  // draw the text and the point
  paint.setTextAlign(Paint.Align.LEFT);

  canvas.drawText("Head Angel = " + strangle, 10, 20, paint);

  strnote = edtnote.getText().toString();
  if (TextUtils.isEmpty(strnote)) {
   strnote = "Note";
  }
  canvas.drawText(strnote, 10, 50, paint);
  paint.setTextAlign(Paint.Align.RIGHT);

  canvas.drawText("www.moovemtb.com", width-60, 50, paint);
  canvas.drawText("Head Angel App", width-60, 20, paint);

  canvas.drawPoint(30.0f, height/2, paint);
  return bmOverlay;
 }

这可能会帮助您,这里需要使用所需字符串更改函数,并传递您的背景<代码>可绘制的图像, 并使用
画布
调整字体大小、文本颜色和对齐方式,创建一个
位图
并进行检查

private Bitmap getThumb(String strangle, String strnote, int width, int height) {

  //Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.transperet_bg);
  Bitmap bmOverlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

  Canvas canvas = new Canvas(bmOverlay);
  Paint paint = new Paint();

  paint.setColor(Color.WHITE);
  paint.setTextSize(20);
  paint.setFlags(Paint.ANTI_ALIAS_FLAG);
  // if the background image is defined in main.xml, omit this line
  canvas.drawARGB(140, 0, 0, 0);
  //canvas.drawBitmap(mBitmap, 0, 0, null);
  // draw the text and the point
  paint.setTextAlign(Paint.Align.LEFT);

  canvas.drawText("Head Angel = " + strangle, 10, 20, paint);

  strnote = edtnote.getText().toString();
  if (TextUtils.isEmpty(strnote)) {
   strnote = "Note";
  }
  canvas.drawText(strnote, 10, 50, paint);
  paint.setTextAlign(Paint.Align.RIGHT);

  canvas.drawText("www.moovemtb.com", width-60, 50, paint);
  canvas.drawText("Head Angel App", width-60, 20, paint);

  canvas.drawPoint(30.0f, height/2, paint);
  return bmOverlay;
 }

请试试这个,它很好用

BufferedImage img1 = ImageIO.read(new File(pathtoImage)); //first image
BufferedImage img2 = ImageIO.read(new File(pathtoOverlay)); //overlay text image
BufferedImage combinedImage = new BufferedImage(img1.getWidth(),img1.getHeight(),BufferedImage.TYPE_INT_RGB);

    Graphics g = combinedImage.getGraphics();

    g.drawImage(img1, 0, 0, null);
    g.drawImage(img2, 0, 0, null);
    ImageIO.write(combinedImage,"JPG",new File(pathToBeSaved,"combined.jpg");

请试试这个,它很好用

BufferedImage img1 = ImageIO.read(new File(pathtoImage)); //first image
BufferedImage img2 = ImageIO.read(new File(pathtoOverlay)); //overlay text image
BufferedImage combinedImage = new BufferedImage(img1.getWidth(),img1.getHeight(),BufferedImage.TYPE_INT_RGB);

    Graphics g = combinedImage.getGraphics();

    g.drawImage(img1, 0, 0, null);
    g.drawImage(img2, 0, 0, null);
    ImageIO.write(combinedImage,"JPG",new File(pathToBeSaved,"combined.jpg");

你需要在这里检查我的答案,首先谢谢,但是MKJParekh它看起来与上面的图片不一样我想在fitxy中将文本图片放在中间,背景图片如果你将EditText(或带有文本图片的imageview)的重心设为中间,那么结果图片将与上面的一样你需要在这里检查我的答案,首先谢谢,但是MKJParekh它看起来和上面的图像不一样我想把文本图像放在中间,背景图像放在Fitxy中如果你把你的EditText(或带有文本图像的imageview)的重心放在中间,那么结果图像就会和上面的一样。请你解释一下。白色图像是什么意思?请确保您正在捕获根framlayout,并且这两个图像位于framlayout内,请详细解释。白色图像是什么意思?请确保捕获的是根framlayout,并且这两个图像位于framlayout内部