Android Java中如何将位图转换为图像

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


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

我需要original_with_water_mark bitmap将其转换为要存储的图像。但我不知道如何将该位图转换为图像


因为在运行函数中,在开始时看到我需要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();

                    }

                }

            }

        }


ibeautiful
浏览 149回答 2
2回答

慕姐4208626

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();    }}

慕娘9325324

您可以将其以任何图像格式保存在本地存储中。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;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java