Image overlay with camera captured image in androi

2019-05-02 17:22发布

问题:

I need to take a picture with the camera and at the same time show an overlay image on top of the camera view. After the picture is taken, i need to save what the user saw while taking the picture. Can anyone suggest me? Please.

回答1:

  public void onPictureTaken(byte[] data, Camera camera){
  Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

  wid = cameraBitmap.getWidth();
  hgt = cameraBitmap.getHeight();


 Bitmap newImage = Bitmap.createBitmap(wid , hgt , Bitmap.Config.ARGB_8888);

  Canvas canvas = new Canvas(newImage);

  canvas.drawBitmap(cameraBitmap, 0f, 0f, null);

  Drawable drawable = getResources().getDrawable(R.drawable.d);
  drawable.setBounds(20 ,20, 260, 160);
  drawable.draw(canvas);

  File storagePath = new File(Environment.getExternalStorageDirectory() + "/Vampire Photos/"); 
  storagePath.mkdirs(); 

  File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");

  try
  {
    FileOutputStream out = new FileOutputStream(myImage);
    newImage.compress(Bitmap.CompressFormat.JPEG, 90, out);


    out.flush();
    out.close();
  }
  catch(FileNotFoundException e)
  {
   Log.d("In Saving File", e + "");
  }
  catch(IOException e)
  {
    Log.d("In Saving File", e + "");
  }  

  camera.startPreview();

  drawable = null;

  newImage.recycle();
  newImage = null;

  cameraBitmap.recycle();
  cameraBitmap = null;
 } };