我浏览了其他一些帖子,似乎找不到答案。当我运行应用程序并拍摄第一张照片时,用于存储它的类范围数组会获得位图的正确值。然而,当我去拍第二张照片时,它只是覆盖了第一张,第二张空白。我猜测这与重写的方法有关,但我不确定。
这是我的代码:
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();
}
}
}
非常感谢您的帮助。
可以编写类似的函数
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();
}
}
}
因为您只是替换通过传递值获得的引用,所以您没有更改调用方方法中的引用。