2016-03-23 55 views
11

我使用下面的代碼用相機拍攝照片。我不想保存,而是將它編碼爲Base64,然後將它傳遞給另一個API作爲輸入。我看不到方法,如何修改代碼在Base64而不是常規文件中拍照。拍照並轉換爲Base64

public class CameraDemoActivity extends Activity { 
    int TAKE_PHOTO_CODE = 0; 
    public static int count = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; 
     File newdir = new File(dir); 
     newdir.mkdirs(); 

     Button capture = (Button) findViewById(R.id.btnCapture); 
     capture.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       count++; 
       String file = dir+count+".jpg"; 
       File newfile = new File(file); 
       try { 
        newfile.createNewFile(); 
       } 
       catch (IOException e) 
       { 
       } 

       Uri outputFileUri = Uri.fromFile(newfile); 

       Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 

       startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { 
      Log.d("CameraDemo", "Pic saved"); 
     } 
    } 
} 

我嘗試用下面的代碼將圖像轉換爲Base64

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) 
{ 
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); 
    image.compress(compressFormat, quality, byteArrayOS); 
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT); 
} 

上面描述的應該比保存圖像更直接和更簡單,然後尋找圖像進行編碼。

+0

'我嘗試使用下面的代碼將圖像轉換爲Base64 '。沒有代碼是用於將位圖轉換爲base64。你有一個.jpg文件。沒有位圖。 – greenapps

+0

'newfile.createNewFile();'。刪除。相機應用程序將創建該文件。 – greenapps

+0

'在Base64而不是普通文件中拍照'。 ??無法將base64與文件進行比較。 – greenapps

回答

21

試試這個:
ImageUri爲位圖:用base64

@Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 

      if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { 
       final Uri imageUri = data.getData(); 
       final InputStream imageStream = getContentResolver().openInputStream(imageUri); 
       final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); 
       String encodedImage = encodeImage(selectedImage); 
      } 
     } 

編碼位圖

private String encodeImage(Bitmap bm) 
    { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.JPEG,100,baos); 
     byte[] b = baos.toByteArray(); 
     String encImage = Base64.encodeToString(b, Base64.DEFAULT); 

     return encImage; 
    } 

從文件路徑編碼爲Base64

private String encodeImage(String path) 
    { 
     File imagefile = new File(path); 
     FileInputStream fis = null; 
     try{ 
      fis = new FileInputStream(imagefile); 
     }catch(FileNotFoundException e){ 
      e.printStackTrace(); 
     } 
     Bitmap bm = BitmapFactory.decodeStream(fis); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.JPEG,100,baos); 
     byte[] b = baos.toByteArray(); 
     String encImage = Base64.encodeToString(b, Base64.DEFAULT); 
     //Base64.de 
     return encImage; 

    } 

輸出: enter image description here

+0

此代碼從一個jpg文件製作位圖。然後位圖再次一致爲JPG。最後這個jpg是base64編碼的。更好地省略位圖。只需base64直接對.jpg文件進行編碼。 – greenapps

+0

此外,相機應用程序還保存了一個文件。 OP要求沒有文件的東西。 – greenapps

+0

但首先,我看不到該文件。我正在使用TC來搜索它,但它不是。 – plaidshirt

0

我已經寫了我這樣的代碼:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Camera mCamera = Camera.open(); 
     mCamera.startPreview();// I don't know why I added that, 
           // but without it doesn't work... :D 

     mCamera.takePicture(null, null, mPicture); 
    } 

    private Camera.PictureCallback mPicture = new Camera.PictureCallback() { 

     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      System.out.println("***************"); 
      System.out.println(Base64.encodeToString(data, Base64.DEFAULT)); 
      System.out.println("***************"); 
     } 
    }; 
} 

它完美...

+2

雖然這段代碼可能會回答這個問題,但提供關於如何解決問題和/或爲什麼解決問題的附加背景可以提高答案的長期價值。請閱讀此[如何回答](http://stackoverflow.com/help/how-to-answer)以提供高質量的答案。 – thewaywewere