0

I'd like to combine two byte arrays which represent images (so they're the same size) into a third byte array which will be the average of the two starting images. Here's the code I'm trying at the moment:

        byte[] facemash = new byte[data1.length];

        for (int i=0; i < data1.length; ++i){
            facemash[i] = ((data1[i]/2)+(data2[i]/2));
        } 

What I'm getting in Eclipse is an error telling me 'Type mismatch: cannot convert from int to byte.' Can anyone see where I'm going wrong here?

Many thanks

3 Answers 3

1

java converts bytes to ints in arithmetic ops. Just cast the result to (byte) since you know it can't overflow.

Sign up to request clarification or add additional context in comments.

Comments

1
facemash[i] = (byte)((data1[i]/2)+(data2[i]/2));

2 Comments

Thanks, but this is still giving me the same error (I've already declared data1 and data2 as bytes in the code)
I changed my answer, then it should be ok.
0

try:

facemash[i] = (byte)((byte)(data1[i]/2)+(byte)(data2[i]/2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.