在android中将字节[]数组转换为字符串时发生异常
//其中imgdata是方法GetRotateImage(imgdata)所需的字符串 //我自己获取图片的方法在android中将字节[]数组转换为字符串时发生异常,android,Android,//其中imgdata是方法GetRotateImage(imgdata)所需的字符串 //我自己获取图片的方法 float rotation =getRotatedImage(imgdata); //在拍摄的照片中,我正在将字节数据转换为字符串 private float getRotatedImage(String imgdata2) { try { ExifInterface exif = new ExifInterface(imgdata2);
float rotation =getRotatedImage(imgdata);
//在拍摄的照片中,我正在将字节数据转换为字符串
private float getRotatedImage(String imgdata2) {
try {
ExifInterface exif = new ExifInterface(imgdata2);
int rotation = (int)exifOrientationToDegrees(
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL));
return rotation;
} catch (IOException e) {
Log.e("CAMERA IMAGE", "Error checking exif", e);
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
return 0f;
}
//这里是我的方法,以便转换为字符串
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
Log.i("CAMERA", "ON PICTURE TAKEN");
int length = arg0.length;
if (length != 0) {
//bitimage = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
finalimage = arg0;
if(finalimage!=null)
{
imgdata=converttostring(finalimage);
}
//compressimageforshow();
Toast.makeText(getApplicationContext(), "IMAGE PROCESSING DONE", 0)
.show();
} else {
Toast.makeText(getApplicationContext(), "NO IMAGE ", 0).show();
}
arg1.startPreview();
//shutterButton.setEnabled(false);
show_image_bt.setEnabled(true);
}
//以下是我的logcat异常详细信息:
private String converttostring(byte[] finalimage2) {
// String basestr=Base64.encodeToString(finalimage2, Base64.NO_WRAP);
return new String(finalimage2);
}
//那么,有谁能告诉我该怎么做吗?我也尝试过android类的Base64
01-22 18:06:48.808: I/ACTIVITY(344): ON CREATE
01-22 18:06:48.808: I/ACTIVITY(344): ON RESUME
01-22 18:06:49.348: I/CAMERA(344): ON SURFACE CHANGE
01-22 18:06:53.018: I/CAMERA(344): ON PICTURE TAKEN
01-22 18:06:56.188: E/(344): can't open '������JFIF����`��`��������fExif����II*������������������>��������������F������(��������������1��������N��������������`������������`������������Paint.NET v3.36������C��
01-22 18:06:56.188: E/(344):
01-22 18:06:56.188: E/(344):
01-22 18:06:56.188: E/(344):
����C
�������@"��������������������������
01-22 18:06:56.188: E/(344): �����������}��!1AQa"q2���#B��R��$3br�
01-22 18:06:56.188: E/(344): %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������������������
01-22 18:06:56.188: E/(344): ���������w��!1AQaq"2�B���� #3R�br�
01-22 18:06:56.188: E/(344): $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�����������������
其中imgdata是string,finalimage是字节数组
等待回复。请原谅我的语言和编码风格
//这里我要提到的是为其生成异常的按钮单击事件
imgdata=Base64.encodeToString(finalimage,Base64.NO_WRAP);
单击此按钮时,将生成异常。
//在resizeimagefroshow()中调用GetRotateImage()并在之后创建新位图
用于显示的图像
//代码如下:
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.show_image:
show_image_imgvw.setVisibility(View.VISIBLE);
retake_photo_bt.setEnabled(true);
show_image_bt.setEnabled(false);
preview.setVisibility(View.INVISIBLE);
if (finalimage != null) {
resizeimageforshow();
show_image_imgvw.setImageBitmap(bitimage);
Toast.makeText(getApplicationContext(), "IMAGE DISPLAYED", 0)
.show();
正如@njzk2提到的,将二进制图像数据转换为字符串没有任何意义。您需要的是传递到
ExifInterface
构造函数中的文件名字符串
因此,首先必须将图像数据保存到文件中:
private void resizeimageforshow() {
if (finalimage != null) {
// bitimage=BitmapFactory.decodeByteArray(finalimage, 0,
// finalimage.length);
// show_image_imgvw.setImageBitmap(bitimage);
Bitmap myimage1 = BitmapFactory.decodeByteArray(finalimage, 0,
finalimage.length);
// imgdata=Base64.encodeToString(finalimage,Base64.NO_WRAP);
android.graphics.Matrix matrix = new android.graphics.Matrix();
float rotation = getRotatedImage(imgdata);
if (rotation != 0f) {
matrix.preRotate(rotation);
}
bitimage = Bitmap.createBitmap(myimage1, 0, 0, myimage1.getWidth(),
myimage1.getHeight(), matrix, true);
然后您可以将ExifInterface
与文件名一起使用
如果要直接从流中读取元数据,则必须使用第三方库。请参阅。如果您更改imgdata=converttostring(finalimage);to imgdata=新字符串(finalimage);是否收到错误?返回新字符串(finalimage2)代码>这没有意义。不能将表示图像的字节[]直接转换为字符串Base64版本更为合理,测试如果(finalimage!=null)
没有用,因为您访问arg0.length
之前的几行(finalimage==null=>arg0==null=>arg0.length)我不明白的是,在哪里会出现无法打开JFIF错误?我无法在拍摄后直接保存文件,因为我必须给图片添加一些颜色或效果。其次,我的目标是使用三星相机以纵向模式拍摄图像,图片应按其外观显示
File file = new File(getExternalCacheDir(), "camera_picture.jpg");
OutputStream os = new FileOutputStream(file.toString());
try {
os.write(finalimage);
} finally {
os.close();
}