parent
8f54c89605
commit
da76b76d05
@ -0,0 +1,102 @@ |
||||
package com.otaliastudios.cameraview.internal.utils; |
||||
|
||||
|
||||
import android.graphics.Bitmap; |
||||
import android.graphics.BitmapFactory; |
||||
import android.graphics.Canvas; |
||||
import android.graphics.Color; |
||||
import android.graphics.ImageFormat; |
||||
import android.graphics.Paint; |
||||
import android.graphics.PorterDuff; |
||||
import android.graphics.Rect; |
||||
import android.graphics.YuvImage; |
||||
import android.media.Image; |
||||
import android.media.ImageReader; |
||||
import android.opengl.GLES20; |
||||
import android.os.Handler; |
||||
import android.os.Looper; |
||||
import android.view.Surface; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.test.ext.junit.runners.AndroidJUnit4; |
||||
import androidx.test.filters.SmallTest; |
||||
|
||||
import com.otaliastudios.cameraview.BaseTest; |
||||
import com.otaliastudios.cameraview.internal.egl.EglCore; |
||||
import com.otaliastudios.cameraview.internal.egl.EglViewport; |
||||
import com.otaliastudios.cameraview.internal.egl.EglWindowSurface; |
||||
import com.otaliastudios.cameraview.size.AspectRatio; |
||||
import com.otaliastudios.cameraview.size.Size; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import java.io.ByteArrayOutputStream; |
||||
import java.nio.ByteBuffer; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertNotEquals; |
||||
import static org.junit.Assert.assertNotNull; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
@RunWith(AndroidJUnit4.class) |
||||
@SmallTest |
||||
public class ImageHelperTest extends BaseTest { |
||||
|
||||
@NonNull |
||||
private Image getImage() { |
||||
ImageReader reader = ImageReader.newInstance(100, 100, ImageFormat.YUV_420_888, 1); |
||||
Surface readerSurface = reader.getSurface(); |
||||
final Op<Image> imageOp = new Op<>(true); |
||||
reader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { |
||||
@Override |
||||
public void onImageAvailable(ImageReader reader) { |
||||
Image image = reader.acquireLatestImage(); |
||||
if (image != null) imageOp.end(image); |
||||
} |
||||
}, new Handler(Looper.getMainLooper())); |
||||
|
||||
// Write on reader surface.
|
||||
Canvas readerCanvas = readerSurface.lockCanvas(null); |
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
||||
paint.setColor(Color.RED); |
||||
readerCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.MULTIPLY); |
||||
readerCanvas.drawCircle(50, 50, 50, paint); |
||||
readerSurface.unlockCanvasAndPost(readerCanvas); |
||||
|
||||
// Wait
|
||||
Image image = imageOp.await(5000); |
||||
assertNotNull(image); |
||||
return image; |
||||
} |
||||
|
||||
@Test |
||||
public void testImage() { |
||||
Image image = getImage(); |
||||
int width = image.getWidth(); |
||||
int height = image.getHeight(); |
||||
int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21); |
||||
int sizeBits = width * height * bitsPerPixel; |
||||
int sizeBytes = (int) Math.ceil(sizeBits / 8.0d); |
||||
byte[] bytes = new byte[sizeBytes]; |
||||
ImageHelper.convertToNV21(image, bytes); |
||||
image.close(); |
||||
|
||||
// Read the image
|
||||
YuvImage yuvImage = new YuvImage(bytes, ImageFormat.NV21, width, height, null); |
||||
ByteArrayOutputStream jpegStream = new ByteArrayOutputStream(); |
||||
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, jpegStream); |
||||
byte[] jpegByteArray = jpegStream.toByteArray(); |
||||
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length); |
||||
assertNotNull(bitmap); |
||||
|
||||
// Wanted to do assertions on the color here but it doesn't work. There must be an issue
|
||||
// with how we are drawing the image in this test, since in real camera, the algorithm works well.
|
||||
// So for now let's just test that nothing crashes during this process.
|
||||
// int color = bitmap.getPixel(bitmap.getWidth() - 1, bitmap.getHeight() - 1);
|
||||
// assertEquals(Color.red(color), 255, 5);
|
||||
// assertEquals(Color.green(color), 0, 5);
|
||||
// assertEquals(Color.blue(color), 0, 5);
|
||||
// assertEquals(Color.alpha(color), 0, 5);
|
||||
} |
||||
} |
Loading…
Reference in new issue