注:獲取一個讀y-made調色板,具有此功能here。
阿諾德的解決方案讓我興奮和急躁,所以這裏有一個改進。如果不研究他的代碼,我無法做到這一點。這個版本似乎更可靠,不太容易出現超時錯誤,但說實話,我根本不知道Java,所以任何改進都是值得歡迎的。
最重要的是:這個版本直接上傳到stack.imgur.com
,所以在StackOverflow中使用這裏是安全的,而不必擔心上傳的圖片會在一段時間後消失。
我提供三個功能:
stackImage
上傳的表達,導出爲PNG,並返回URL
stackMarkdown
返回降價,準備複製
stackCopyMarkdown
副本降價到剪貼板
下一步:創建一個調色板按鈕,自動爲所選圖形自動執行此操作e筆記本。對代碼的改進非常受歡迎。
Needs["JLink`"]
stackImage::httperr = "Server returned respose code: `1`";
stackImage::err = "Server returner error: `1`";
stackImage[g_] :=
Module[
{getVal, url, client, method, data, partSource, part, entity, code,
response, error, result},
(* this function attempts to parse the response fro the SO server *)
getVal[res_, key_String] :=
With[{k = "var " <> key <> " = "},
StringTrim[
[email protected][[email protected][res, StringMatchQ[#, k ~~ ___] &],
k ~~ v___ ~~ ";" :> v],
"'"]
];
data = ExportString[g, "PNG"];
JavaBlock[
url = "https://stackoverflow.com/upload/image";
client = JavaNew["org.apache.commons.httpclient.HttpClient"];
method = JavaNew["org.apache.commons.httpclient.methods.PostMethod", url];
partSource = JavaNew["org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource", "mmagraphics.png", MakeJavaObject[data]@toCharArray[]];
part = JavaNew["org.apache.commons.httpclient.methods.multipart.FilePart", "name", partSource];
[email protected]["image/png"];
entity = JavaNew["org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity", {part}, [email protected][]];
[email protected][entity];
code = [email protected][method];
response = [email protected][];
]
If[code =!= 200, Message[stackImage::httperr, code]; Return[$Failed]];
response = StringTrim /@ StringSplit[response, "\n"];
error = getVal[response, "error"];
result = getVal[response, "result"];
If[StringMatchQ[result, "http*"],
result,
Message[stackImage::err, error]; $Failed]
]
stackMarkdown[g_] := "![Mathematica graphics](" <> stackImage[g] <> ")"
stackCopyMarkdown[g_] := Module[{nb, markdown},
markdown = Check[stackMarkdown[g], $Failed];
If[markdown =!= $Failed,
nb = NotebookCreate[Visible -> False];
NotebookWrite[nb, Cell[markdown, "Text"]];
SelectionMove[nb, All, Notebook];
FrontEndTokenExecute[nb, "Copy"];
NotebookClose[nb];
]
]
更新:
這裏有一個按鈕,將顯示選擇的預覽,並會提供上傳(或取消)。它要求定義以前的功能。
Button["Upload to SO",
Module[{cell = [email protected][], img},
If[cell =!= {}, img = Rasterize[cell];
MessageDialog[
Column[{"Upload image to StackExchange sites?",
img}], {"Upload and copy MarkDown" :> stackCopyMarkdown[img],
"Cancel" :> Null}, WindowTitle -> "Upload to StackExchange"]]]]
可惜我不能把按鈕調色板(CreatePalette
),因爲面板的尺寸會影響光柵化。歡迎解決這個問題。
更新2:
基於答案this question,這裏的工作僅限於Windows的面板按鈕:
button = Button["Upload to SO",
Module[{sel},
FrontEndExecute[
FrontEndToken[FrontEnd`SelectedNotebook[], "CopySpecial", "MGF"]];
sel = Cases[[email protected]Notebook[],
RasterBox[data_, ___] :>
Image[data, "Byte", ColorSpace -> "RGB", Magnification -> 1],
Infinity];
If[sel =!= {},
With[{img = First[sel]},
MessageDialog[
Column[{"Upload image to StackExchange sites?",
img}], {"Upload and copy MarkDown" :> stackCopyMarkdown[img],
"Cancel" :> Null}, WindowTitle -> "Upload to StackExchange"]
]
]
]
]
CreatePalette[button]
警告:即使您單擊取消在它破壞了剪貼板內容預覽框。
請注意,上傳到'HTTP:// i.stack.imgur.com /'是比較困難的(你要「驅動」 SO接口) –
@belisarius哎呀,我沒有意識到的StackOverflow使用一個單獨的imgur網站...在主imgur網站上的圖像可能不會永久保存,所以也許它不是一個好主意,用於SO http://imgur.com/faq#long(至少1個視圖/需要6個月才能保存) – Szabolcs
手動發佈圖形並不是很難(在V8中)。右鍵單擊圖形,選擇「圖像另存爲...」。然後,文件對話框將打開上次的位置,通常是我的桌面,其中名爲output.png的文件已經在等待其下一個版本被覆蓋。只需點擊兩次,再點擊兩次,並將其發佈在我的SO回答框中。最多需要15秒。獲取Markdown行並粘貼將花費大約相同的時間。 –