plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

How does clipboard manager's writeImage function work?

Open kaanyalova opened this issue 1 year ago • 11 comments

If i try to write images like the example:

import { writeImage } from '@tauri-apps/plugin-clipboard-manager';
const buffer = [
  // A red pixel
  255, 0, 0, 255,

 // A green pixel
  0, 255, 0, 255,
];
await writeImage(buffer);

I get an error on the webview console Unhandled Promise Rejection: expected RGBA image data, found raw bytes

If I try to convert an image to RGBA data in rust then pass the buffer I still get the same error

#[tauri::command(async)]
#[specta::specta]
pub fn image_path_to_rgba_bytes(path: &str) -> Vec<u8> {
    let img = image::open(path).unwrap().to_rgba8();
    img.into_raw()
}
const rawImage = await commands.imagePathToRgbaBytes(imagePath);
await writeImage(rawImage);
// Unhandled Promise Rejection: expected RGBA image data, found raw bytes

kaanyalova avatar Dec 14 '24 16:12 kaanyalova

@kaanyalova Have you gotten a solution to this yet, I'm currently having this same issue. If i should use the example from the writeImage method, i get the expected RGBA image data, found raw bytes error. Sometime when i even pass a string i.e a base64 string i get this too expected RGBA image data,found File Path`.

@FabianLars Can you please look in this?

Slenzy2 avatar Jan 14 '25 15:01 Slenzy2

I'm not really familiar with this function but from a quick look it looks like the example is wrong. Pixel data like this is only supported if you use the Image type. Something like this i think (typed in the web interface so may contain errors)

import { Image } from "@tauri-apps/api/image";
import { writeImage } from "@tauri-apps/plugin-clipboard-manager";

const pixels = [
  // A red pixel
  255, 0, 0, 255,

 // A green pixel
  0, 255, 0, 255,
];
const image = await Image.new(pixels, 2, 1);
await writeImage(image);

Sometime when i even pass a string i.e a base64 string i get this too expected RGBA image data,found File Path`.

base64 is not supported afaik


Edit: The image size was wrong. I changed 2,2 to 2,1

FabianLars avatar Jan 14 '25 16:01 FabianLars

So I just tried this same example code you gave above.

  1. It threw a typescript error Screenshot 2025-01-14 at 5 07 27 PM

  2. I ignored the typescript error and tried testing the writing of the image to the clipboard but still got this error: invalid args imagefor commandwrite_image: data did not match any variant of untagged enum JsImage

Slenzy2 avatar Jan 14 '25 16:01 Slenzy2

I just tried it and i don't have any type issues and it writes the image correctly. Can you share the output of the tauri info command?

FabianLars avatar Jan 14 '25 16:01 FabianLars

this is the Tauri info

`[✔] Environment - OS: Mac OS 15.1.1 arm64 (X64) ✔ Xcode Command Line Tools: installed ✔ rustc: 1.80.0 (051478957 2024-07-21) ✔ cargo: 1.80.0 (376290515 2024-07-16) ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24) ✔ Rust toolchain: stable-aarch64-apple-darwin (environment override by RUSTUP_TOOLCHAIN) - node: 20.10.0 - yarn: 1.22.22 - npm: 10.5.1

[-] Packages - tauri 🦀: 2.1.1 - tauri-build 🦀: 2.0.3 - wry 🦀: 0.47.2 - tao 🦀: 0.30.8 - tauri-cli 🦀: 2.0.3

[-] Plugins - tauri-plugin-notification 🦀: 2.0.1 - tauri-plugin-shell 🦀: 2.0.1 - tauri-plugin-os 🦀: 2.0.1 - tauri-plugin-dialog 🦀: 2.0.1 - tauri-plugin-autostart 🦀: 2.0.1 - tauri-plugin-single-instance 🦀: 2.0.1 - tauri-plugin-global-shortcut 🦀: 2.0.1 - tauri-plugin-fs 🦀: 2.0.1

[-] App - build-type: bundle - CSP: default-src 'self' 'unsafe-inline' data: - frontendDist: ../build - devUrl: http://localhost:3000/

[-] iOS - Developer Teams: None`

Slenzy2 avatar Jan 14 '25 16:01 Slenzy2

Hmm, maybe try updating the plugins to 2.2.x ?

FabianLars avatar Jan 14 '25 16:01 FabianLars

Screenshot 2025-01-14 at 5 50 28 PM

You'd notice that for the tauri-plugin-clipboard-manager, the version is already at 2.2.0. I think that is the highest version. I also checked it here: https://www.npmjs.com/package/@tauri-apps/plugin-clipboard-manager?activeTab=versions

Slenzy2 avatar Jan 14 '25 16:01 Slenzy2

yep that's good - it was missing in the tauri info output which i didn't notice cause all were on the same version. What about the JS packages? Or are you using the window.__TAURI__ object?

FabianLars avatar Jan 14 '25 16:01 FabianLars

Or are you using the window.TAURI object?

No I'm not using this

And these are the js packages that includes the clipboard manager: Screenshot 2025-01-14 at 6 01 14 PM

Also i was wondering why it's not included in the plugins list from the tauri info. And i already have it in the cargo.toml as dependecy, i also have it initialized in my lib.rs file. Here; Screenshot 2025-01-14 at 6 04 30 PM

Slenzy2 avatar Jan 14 '25 17:01 Slenzy2

Also i was wondering why it's not included in the plugins list from the tauri info

Maybe because of outdated tauri-cli / @tauri-apps/cli ? 🤔

FabianLars avatar Jan 14 '25 18:01 FabianLars

Make sure you enable "image-png" in the Cargo.toml file; then Rust will be able to process the buffer.

[dependencies]
tauri = { version = "2", features = ["image-png"] }

kimulisiraj avatar Apr 03 '25 12:04 kimulisiraj