如何在 android Java 中将位图转换为图像

How to convert Bitmap to image in android Java

我想将我的位图转换为图像,而不是可绘制的,我已经看到一些例子,他们正在将位图转换为可绘制的,但我需要 media.image(image) 然后我正在处理该图像具有进一步的逻辑。帮我解决这个问题 简而言之,我需要将位图转换为图像。

 Bitmap original_with_water_mark= addWatermark(original_image_bitmap,water_mark_bitmap,300);

我需要这个original_with_water_mark bitmap转换成图像来存储。但我不知道如何将该位图转换为图像

因为在 运行 函数中 see at start 我需要mImage这是我必须存储的图像

 @Override
        public void run() {

            ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
            byte[] bytes = new byte[buffer.remaining()];
            buffer.get(bytes);
            FileOutputStream output = null;
            try {
                output = new FileOutputStream(mFile);
                output.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                mImage.close();
                if (null != output) {
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

yu can use:-

    private void save(){ 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH);
    filename = sdf.format(new Date());

    try {
        String path = getApplicationContext().getFilesDir().getPath();
        OutputStream fOut = null;
        File file = new File(path, "MYFile"//your file name);
        if (!file.exists()) {
            file.mkdirs();
        }

        File file2 = new File(file, filename + ".png");
        fOut = new FileOutputStream(file2);


        //your bitmap
        original_with_water_mark.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    }
}

您可以将其以任何图像格式保存在本地存储中。

private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
    Log.d(TAG,
            "Error while creating media file, Please ask for storage permission");
    return;
} 
try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    image.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
} catch (FileNotFoundException e) {
    Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
    Log.d(TAG, "Error accessing file: " + e.getMessage());
}  

}

private  File getOutputMediaFile(){

File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
        + "/Android/data/"
        + getApplicationContext().getPackageName()
        + "/Files"); 


if (! mediaStorageDir.exists()){
    if (! mediaStorageDir.mkdirs()){
        return null;
    }
}  
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
    String mImageName="MI_"+ timeStamp +".jpg";
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);  
return mediaFile;

}