[英]How to get bitmap to byte array android?
I've looked through some other posts and can't seem to find an answer. 我浏览了其他一些帖子,但似乎找不到答案。 When I run the app and take the first picture, the classwide array used to store it gets the correct value of the bitmap.
当我运行该应用程序并拍摄第一张照片时,用于存储它的全类数组将获得位图的正确值。 However, when I go to take the second picture, it just overwrites the first one and leaves the second one blank.
但是,当我拍摄第二张照片时,它只会覆盖第一张照片,而将第二张照片留空。 I am guessing that this has something to do with the overridden method but I'm not sure.
我猜想这与重写的方法有关,但是我不确定。
Here's my code: 这是我的代码:
package vikin.example.com.picturecomparisonv2;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
public byte[] firstImage = null;
public byte[] secondImage = null;
long hits;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
}
public void takePicture(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
try {
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
setByteArrays(imageBitmap);
} catch(NullPointerException e) {
e.printStackTrace();
}
}
}
public void analyze(View view) {
if(firstImage != null && secondImage != null) {
int length = 0;
if(firstImage.length <= secondImage.length) {
length = firstImage.length;
} else if(firstImage.length >= secondImage.length) {
length = secondImage.length;
}
for(int i = 0; i < length; i++) {
if(firstImage[i] == secondImage[i]) {
hits++;
}
}
if(hits == length) {
Toast.makeText(this, "Images are exaclty identical",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Images are different", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Need 1 or more images", Toast.LENGTH_LONG).show();
}
}
public void setByteArrays(Bitmap bitmap) {
try {
if(firstImage == null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] firstImageArray = stream.toByteArray();
firstImage = firstImageArray;
} else if(secondImage == null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] secondImageArray = stream.toByteArray();
secondImage = secondImageArray;
} else {
Toast.makeText(this, "Only two pictures allowed", Toast.LENGTH_LONG).show();
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
}
Any help is much appreciated. 任何帮助深表感谢。
Can write the function like this 可以这样写函数
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
public byte[] firstImage = null;
public byte[] secondImage = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
long hits;
public void setByteArrays(Bitmap bitmap) {
try {
if(firstImage == null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
firstImage = stream.toByteArray();
} else if(secondImage == null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
secondImage = stream.toByteArray();
} else {
Toast.makeText(this, "Only two pictures allowed", Toast.LENGTH_LONG).show();
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
}
Because you are only replacing the reference that you obtained by pass-by-value, so you are not changing the reference in the caller method. 因为您只替换通过值传递获得的引用,所以您不会在调用方方法中更改引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.