Android Image Add Watermark Picture and Save Picture to File Storage Implementation Code

  • 2021-09-12 02:05:53
  • OfStack

The specific code is as follows:


package zhangphil.test; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
public class JavaActivity extends AppCompatActivity { 
 @Override 
 protected void onCreate(@Nullable Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.drawable_activity); 
  findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    add(); 
   } 
  }); 
 } 
 private void add() { 
  new Thread(new Runnable() { 
   @Override 
   public void run() { 
    File zhang = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "zhang.jpg"); 
    try { 
     // Original picture.  
     Bitmap bitmap1 = BitmapFactory.decodeStream(new FileInputStream(zhang)); 
     // Watermark.  
     Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.logo); 
     // After adding watermark to the original picture, a new file is formed.  
     File zhangphil = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "zhangphil.jpg"); 
     if (!zhangphil.exists()) 
      zhangphil.createNewFile(); 
     // A new image formed after adding watermark to the original image Bitmap . Start adding at the far left and top of the original picture.  
     // If it is the middle or bottom, it needs to be calculated x,y Gets or sets the coordinate position of the.  
     Bitmap newbitmap = addImageWatermark(bitmap1, bitmap2, 0, 0); 
     // Put the watermarked Bitmap Save to file.  
     save(newbitmap, zhangphil, Bitmap.CompressFormat.JPEG, true); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
   } 
  }).start(); 
 } 
 /** 
  *  Add a picture watermark.  
  * 
  * @param src   Source picture  
  * @param watermark  Image watermarking  
  * @param x    Starting coordinates x 
  * @param y    Starting coordinates y 
  * @return  Picture with picture watermark  
  */ 
 public static Bitmap addImageWatermark(Bitmap src, Bitmap watermark, int x, int y) { 
  Bitmap retBmp = src.copy(src.getConfig(), true); 
  Canvas canvas = new Canvas(retBmp); 
  canvas.drawBitmap(watermark, x, y, null); 
  return retBmp; 
 } 
 /** 
  *  Save pictures to file File .  
  * 
  * @param src   Source picture  
  * @param file  File to save to  
  * @param format  Format  
  * @param recycle  Recycle or not  
  * @return true  Success  false  Failure  
  */ 
 public static boolean save(Bitmap src, File file, Bitmap.CompressFormat format, boolean recycle) { 
  if (isEmptyBitmap(src)) 
   return false; 
  OutputStream os; 
  boolean ret = false; 
  try { 
   os = new BufferedOutputStream(new FileOutputStream(file)); 
   ret = src.compress(format, 100, os); 
   if (recycle && !src.isRecycled()) 
    src.recycle(); 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
  return ret; 
 } 
 /** 
  * Bitmap Object is empty.  
  */ 
 public static boolean isEmptyBitmap(Bitmap src) { 
  return src == null || src.getWidth() == 0 || src.getHeight() == 0; 
 } 
} 

It is necessary to put the source picture Pictures/zhang. jpg and the watermark picture res/drawable/logo. jpg in advance.

Authority:


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Summarize


Related articles: