Combine Images in android

前端 未结 2 1686
猫巷女王i
猫巷女王i 2021-02-10 06:36

how to merge two images in android by programming in java and save in external SD Card or some where else.

2条回答
  •  情深已故
    2021-02-10 07:17

    Try out this code.

    private static final String TAG = "JoinImage";
    private Bitmap mBackImage, mTopImage, mBackground; 
    private BitmapDrawable mBitmapDrawable;
    private static String mTempDir;
    private String mSavedImageName = null; 
    private FileOutputStream mFileOutputStream = null;
    private Canvas mCanvas;
    

    in onCreate()

    //Create folder in SDCard to store newly generated image
    mTempDir = Environment.getExternalStorageDirectory() + "/TestTemp/";
    File mTempFile = new File(mTempDir);
    if(!mTempFile.exists()) {
        mTempFile.mkdirs();
    }
    //File name 
    mSavedImageName = "Test.png";
    //Width = 604, Height = 1024 Change as per your requirement
    mBackground = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    //Put back and top images in your res folder
    mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.launcher);
    mTopImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    
    mCanvas = new Canvas(mBackground);
    mCanvas.drawBitmap(mBackImage, 0f, 0f, null);
    mCanvas.drawBitmap(mTopImage, 12f, 12f, null);
    
    try {
        mBitmapDrawable = new BitmapDrawable(mBackground);
        Bitmap mNewSaving = mBitmapDrawable.getBitmap();
        String FtoSave = mTempDir + mSavedImageName;
        File mFile = new File(FtoSave);
        mFileOutputStream = new FileOutputStream(mFile);
        mNewSaving.compress(CompressFormat.PNG, 95, mFileOutputStream);
        mFileOutputStream.flush();
        mFileOutputStream.close();
    } catch(FileNotFoundException e) {
        Log.e(TAG, "FileNotFoundExceptionError " + e.toString());
    } catch(IOException e) {
        Log.e(TAG, "IOExceptionError " + e.toString());
    }
    Log.i(TAG, "Image Created");
    

    in Manifestadd this uses-permission

提交回复
热议问题