2012-09-18 45 views
0

我有一些Java代碼需要一個屏幕截圖並輸出一個PNG,這是爲了保存到手機的圖片庫。它看起來像這樣:返回一個base64字符串而不是一個png的android應用程序

package org.apache.cordova; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.cordova.api.Plugin; 
import org.apache.cordova.api.PluginResult; 
import org.json.JSONArray; 

import android.graphics.Bitmap; 
import android.os.Environment; 
import android.view.View; 



public class Screenshot extends Plugin { 

    @Override 

    public PluginResult execute(String action, JSONArray args, String callbackId) { 
     // starting on ICS, some WebView methods 
     // can only be called on UI threads 
     final Plugin that = this; 
     final String id = callbackId; 
     super.cordova.getActivity().runOnUiThread(new Runnable() { 
      //@Override 
      public void run() { 
       View view = webView.getRootView(); 

       view.setDrawingCacheEnabled(true); 
       Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); 
       view.setDrawingCacheEnabled(false); 

       try { 
        File folder = new File(Environment.getExternalStorageDirectory(), "Pictures"); 
        if (!folder.exists()) { 
         folder.mkdirs(); 

        } 

        File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png"); 
        System.out.println(folder); 
        System.out.println("screenshot_" + System.currentTimeMillis() + ".png"); 
        FileOutputStream fos = new FileOutputStream(f); 
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
        that.success(new PluginResult(PluginResult.Status.OK), id); 
        System.out.println("get here?"); 

       } catch (IOException e) { 
        that.success(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage()), id); 
        System.out.println("and here?"); 
       } 
      } 
     }); 

     PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); 
     result.setKeepCallback(true); 
     return result; 
    } 

} 

現在的項目我工作的目的,我想不是T保存到畫廊,而是輸出,可以操縱一個base64字符串。我是Java的新手,但在瀏覽一些base64 Java編碼器後發現了這些東西。但似乎有一個名爲Base64OutputStream的android util方法。

我想我需要替換的代碼爲:

FileOutputStream fos = new FileOutputStream(f); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 

起初我想:

Base64OutputStream os = new Base64OutputStream(); 

但它扔了錯誤,那麼我已經玩有關使用的代碼,我的其他位找到。如:

ByteArrayOutputStream os = new ByteArrayOutputStream(); 
OutputStream b64 = new Base64.OutputStream(os); 
ImageIO.write(bi, "png", b64); 
String result = os.toString("UTF-8"); 

這又沒有工作,而不是實現base64編碼庫爲我的項目的一小部分,我想知道是否有人可以點我在正確的方向,或者如果IM告訴我在正確的道路上。我是Java新手,但覺得android提供的Base64OutputStream必須是關鍵。任何人有任何指針?

回答

0

試試這個代碼這應該爲你工作?

 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     byte[] b = baos.toByteArray(); 
     base64String = Base64.encodeToString(b, Base64.DEFAULT); 
+0

base64String無法解析爲變量。我需要在某個地方定義這個嗎? – Bohdi

+0

創建一個字符串變量名稱base64String。 – Deepak

+0

但toByteArray()會創建一個新副本(不必要地)。當然,這不可能是最好的解決方案? – Tom

0

圖像的編碼非常簡單。

編碼源:

Bitmap bm = BitmapFactory.decodeFile(picturePath); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      byte[] byteArray = baos.toByteArray(); 

      encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT); 

    ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

解碼來源:

byte[] decodedString; 
     decodedString = Base64.decode(picture, Base64.DEFAULT); 

     imageView1.setImageBitmap(BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length)); 
+0

當我讀到這段代碼時,如果我正確的話,它會將已經保存到設備的圖像進行編碼? – Bohdi

0

我看到你的Android標記。如果您可以導入org.kobjects.base64包(在中找到ksoap2-android-assembly-2.4-jar -with-dependencies.jar),那麼您可以使用一些實用程序。有了您的PNG圖像的字節數組,你只需要做:

String pngAsBase64String = Base64.encode(/*PNG byte array*/); 

我發現這些工具更容易比Sun公司的Base64編碼使用器和解碼器

相關問題