我已经构建了Web服务,该服务将接收在base64中解码的图像 我编写了这段代码,使我可以从相机拍摄图片并将其放入ImageView 我想拍这张照片并将其转换为base64字符串 有人可以帮忙吗? 这是我的代码
class takepicActivity : AppCompatActivity() {
val CAMERA_REQUEST_CODE = 0
var pic:Bitmap?=null
var encoded_image:String?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_takepic)
val id = intent.getStringExtra("id")
//click on return
findViewById<TextView>(R.id.txt_return).setOnClickListener{
val intent = Intent(applicationContext, HelloActivity::class.java)
intent.putExtra("id",id)
startActivity(intent)
finish()
}
//take the pic
val callCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if(callCameraIntent.resolveActivity(packageManager) != null)
startActivityForResult(callCameraIntent,CAMERA_REQUEST_CODE)
}
fun BitMapToString(bitmap: Bitmap): String {
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100,baos)
val b=baos.toByteArray()
return Base64.encodeToString(b, Base64.DEFAULT)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
CAMERA_REQUEST_CODE -> {
if(resultCode == Activity.RESULT_OK && data != null){
pic_here.setImageBitmap(data.extras.get("data") as Bitmap)
}
else{val intent = Intent(applicationContext, HelloActivity::class.java)
startActivity(intent)
finish() }
}
else -> {Toast.makeText(this,"unrecognized request code", Toast.LENGTH_SHORT).show()}
}}
}
答案 0 :(得分:0)
在一个旧项目中,我完成了以下操作:
public static String getEncodedFileContentBase64(final Context context, final Uri uri) {
String encoded = "";
if (context != null && uri != null) {
InputStream inputStream;
try {
String filePath = getRealPathFromURI(context, uri);
if (!TextUtils.isEmpty(filePath)) {
inputStream = BitmapUtils.getImageFile(filePath);
byte[] buffer = new byte[10 * ONE_KIO];
int bytesRead;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Base64OutputStream base64OutputStream = new Base64OutputStream(outputStream, Base64.DEFAULT);
while ((bytesRead = inputStream.read(buffer)) != -1) {
base64OutputStream.write(buffer, 0, bytesRead);
}
base64OutputStream.close();
encoded = outputStream.toString();
}
} catch (Exception e) {
LogUtils.e(TAG, "Exception when reading image file: " + e.getMessage());
}
}
return encoded;
}
希望对您有帮助
答案 1 :(得分:0)
尝试一下: ImageUri到位图:
@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);
}
}
在base64中编码位图
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;
}
从FilePath编码为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;
}