Android 调用照相机拍照把照片转化为字节数组

本文介绍了Android中将相机拍摄的照片转化为字节数组的两种方法:通过BitmapFactory.decodeByteArray和使用Bitmap的compress方法保存到ByteArrayOutputStream。详细解释了compress方法的参数含义,以及JPG和PNG编码的区别。
摘要由CSDN通过智能技术生成

转化为字节数组的方法有两种:

第一种:

  字节数组data保存Bitmap对象转为字节数组,处理代码:

BitmapFactory.decodeByteArray(data, 0, data.length);

第二种:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data2 = baos.toByteArray();

这里其实很好理解,第二种方法使用了Bitmap的compress方法,一般用于保存一个Bitmap对象,转为字节输出流,但是compress目前编码由两种,比如JPG,一般处理照片和PNG,PNG一般处理带Alpha透明通道的图片,后面的第二个参数是清晰度,一般100是最高,0是最低,这个值越大图片越清晰,同时文件体积越大,JPG和PNG都是压缩的图片,所以和原始的直接通过BitmapFactory.decodeByteArray解码后的大小会有很大的不同。

 

android调用照相机拍照获取照片并做简单剪裁:

1.调用系统的照相机程序
Intent intent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
 startActivityForResult(intent,TAKE_PHOTO_WITH_DATA);

2.在onActivityResult中处理返回的data
final Bitmap photo = data.getParcelableExtra("data");
如果不需要做剪切处理就可以直接使用图片了,比如输出到ImageView上
imageView.setImageBitmap(photo);
如果需要做剪切处理,就继续向下执行

3.做剪切处理
       Intentintent = newIntent("com.android.camera.action.CROP");
       intent.setType("image
   privateButton btnTake = null;
   privateImageView lblImage = null;
   
   privatestatic final int PHOTO_PICKED_WITH_DATA = 3021;
   privatestatic final int CAMERA_WITH_DATA = 3023;
   
   @Override
   public voidonCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       btnTake= (Button)findViewById(R.id.btnTake);
       lblImage= (ImageView)findViewById(R.id.lblImage);
       btnTake.setOnClickListener(newView.OnClickListener(

Java中没有结构体的概念,但可以使用Java中的类来模拟结构体。将字符串换成类对象后,可以使用Java中的序列化机制将类对象换成字节数组,再调用C接口。 例如,假设有以下C结构体: ``` struct MyStruct { int id; char name[20]; float score; }; ``` 在Java中,可以定义一个类来模拟这个结构体: ``` import java.io.Serializable; public class MyStruct implements Serializable { private int id; private String name; private float score; public MyStruct(int id, String name, float score) { this.id = id; this.name = name; this.score = score; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } } ``` 然后,将字符串换成类对象: ``` String str = "1,John,90.5"; String[] parts = str.split(","); int id = Integer.parseInt(parts[0]); String name = parts[1]; float score = Float.parseFloat(parts[2]); MyStruct myStruct = new MyStruct(id, name, score); ``` 接下来,将类对象换成字节数组: ``` ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(myStruct); byte[] bytes = bos.toByteArray(); ``` 最后,调用C接口: ``` native void myFunction(byte[] bytes); myFunction(bytes); ``` 在C中,可以使用以下代码将字节数组换成结构体: ``` #include <stdio.h> struct MyStruct { int id; char name[20]; float score; }; void myFunction(char* bytes, int len) { struct MyStruct myStruct; memcpy(&myStruct, bytes, sizeof(struct MyStruct)); printf("id=%d, name=%s, score=%f\n", myStruct.id, myStruct.name, myStruct.score); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值