FaceFilter icon indicating copy to clipboard operation
FaceFilter copied to clipboard

Cant save Image with overlay

Open SajidAguero2110 opened this issue 7 years ago • 3 comments

I am unable to save image with the overlay on it. I tried creating two separate bitmap and merging them both but its of no help. Can you please tell me how it can be achieved ???

SajidAguero2110 avatar May 23 '17 12:05 SajidAguero2110

take screenshot of root layout u will get black image with overlay image then capture the photo u will get camera image after that send both in next page and show both image one up another after that take screenshot of root layout of second activity.

mCameraSource.takePicture(null, new CameraSource.PictureCallback() { @Override public void onPictureTaken(byte[] bytes) { Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Matrix m = new Matrix(); m.preScale(-1, 1); Bitmap dst = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, false); Bitmap bitmap=getScreenShot(); saveBitmapImage(dst); saveBitmapImage1(bitmap); Log.d("BITMAP", bmp.getWidth() + "x" + bmp.getHeight()); Intent i=new Intent(FaceTrackerActivity.this,Preview.class); startActivity(i); } });

================================================================

cameraImage= (ImageView) findViewById(R.id.cameraImage); frame= (ImageView) findViewById(R.id.frame); goback= (Button) findViewById(R.id.back); save= (Button) findViewById(R.id.save); up= (RelativeLayout) findViewById(R.id.up); Bitmap back = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +"/Temporary/temp_1.png"); Bitmap front = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +"/Temporary/temp_2.png");

    cameraImage.setImageBitmap(back);
    frame.setImageBitmap(front);

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Bitmap image=getScreenShot();
            saveBitmapImage(image);
            File dir = new File(Environment.getExternalStorageDirectory() +"/Temporary");
            try {
                FileUtils.deleteDirectory(dir);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(Preview.this, "Image Saved", Toast.LENGTH_SHORT).show();

        }
    });
    goback.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });
}

////////////////////////////////////////////////Taking Screenshot of final image//////////////////////////////////////////////////////
public Bitmap getScreenShot( ) {
    Bitmap bitmap;
    View view = findViewById(R.id.up);// get ur root view id
    view.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    Bitmap screenshot = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(screenshot);
    view.draw(canvas);
    return screenshot;
}


////////////////////////////////////////////////Saving the screenshot//////////////////////////////////////////////////////
public void saveBitmapImage(Bitmap sourceBitmap) {
    boolean imageSaved = false;
    if (sourceBitmap != null && !sourceBitmap.isRecycled()) {
        File storagePath=null;
        storagePath = new File(Environment.getExternalStorageDirectory() +"/FaceChanger");
        if (!storagePath.exists()) {
            storagePath.mkdirs();
        }
        FileOutputStream out = null;
        Random r = new Random();
        int randomNumber = r.nextInt(1000);
        File imageFile = new File(storagePath, String.format("%s_%d.png","ImageBlender",randomNumber));
        if (imageFile.exists()) {
            boolean isDeleted = imageFile.delete();
            if (isDeleted)
            {
                try {
                    imageFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            out = new FileOutputStream(imageFile);
            imageSaved = sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        } catch (Exception e) {
            // Log.e(SyncStateContract.Constants.LOG_TAG, "Unable to write the image to gallery", e);
        } finally {
            if (out != null) {
                //out.flush();
                //out.close();
            }
        }

    }

devraajdeveloper avatar Sep 18 '17 12:09 devraajdeveloper

Is there any other way to save Image with overlay??

akku20080 avatar Dec 14 '17 10:12 akku20080

hello, you are missing one function saveBitmapImage1

rakeshkhoodeeram avatar May 01 '18 02:05 rakeshkhoodeeram