AndroidImageProxy.java revision b8397360d318edf3093b20b2b102207d76730e1b
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.camera.one.v2.camera2proxy; 18 19import android.graphics.Rect; 20import android.media.Image; 21 22import com.google.common.base.Objects; 23 24import java.nio.ByteBuffer; 25import java.util.ArrayList; 26import java.util.List; 27 28/** 29 * An {@link ImageProxy} backed by an {@link android.media.Image}. 30 */ 31public class AndroidImageProxy implements ImageProxy { 32 33 /** 34 * An {@link ImageProxy.Plane} backed by an 35 * {@link android.media.Image.Plane}. 36 */ 37 public class Plane implements ImageProxy.Plane { 38 private final Image.Plane mPlane; 39 40 public Plane(Image.Plane imagePlane) { 41 mPlane = imagePlane; 42 } 43 44 /** 45 * @see {@link android.media.Image.Plane#getRowStride} 46 */ 47 @Override 48 public int getRowStride() { 49 return mPlane.getRowStride(); 50 } 51 52 /** 53 * @see {@link android.media.Image.Plane#getPixelStride} 54 */ 55 @Override 56 public int getPixelStride() { 57 return mPlane.getPixelStride(); 58 } 59 60 /** 61 * @see {@link android.media.Image.Plane#getBuffer} 62 */ 63 @Override 64 public ByteBuffer getBuffer() { 65 return mPlane.getBuffer(); 66 } 67 68 } 69 70 private final android.media.Image mImage; 71 72 public AndroidImageProxy(android.media.Image image) { 73 mImage = image; 74 } 75 76 /** 77 * @see {@link android.media.Image#getCropRect} 78 */ 79 @Override 80 public Rect getCropRect() { 81 return mImage.getCropRect(); 82 } 83 84 /** 85 * @see {@link android.media.Image#setCropRect} 86 */ 87 @Override 88 public void setCropRect(Rect cropRect) { 89 mImage.setCropRect(cropRect); 90 } 91 92 /** 93 * @see {@link android.media.Image#getFormat} 94 */ 95 @Override 96 public int getFormat() { 97 return mImage.getFormat(); 98 } 99 100 /** 101 * @see {@link android.media.Image#getHeight} 102 */ 103 @Override 104 public int getHeight() { 105 return mImage.getHeight(); 106 } 107 108 /** 109 * <p> 110 * NOTE:This wrapper is functionally correct, but has some performance 111 * implications: it dynamically allocates a small array (usually 1-3 112 * elements) and iteratively constructs each element of this array every 113 * time it is called. This function definitely should <b>NOT</b> be called 114 * within an tight inner loop, as it may litter the GC with lots of little 115 * allocations. However, a proper caching of this object needs to be tied to 116 * the Android Image updates, which would be a little more complex than this 117 * object needs to be. So, just consider the performance when using this 118 * function wrapper. 119 * </p> 120 * @see {@link android.media.Image#getPlanes} 121 */ 122 @Override 123 public List<ImageProxy.Plane> getPlanes() { 124 Image.Plane[] planes = mImage.getPlanes(); 125 126 List<ImageProxy.Plane> wrappedPlanes = new ArrayList<>(planes.length); 127 128 for (int i = 0; i < planes.length; i++) { 129 wrappedPlanes.add(new Plane(planes[i])); 130 } 131 return wrappedPlanes; 132 } 133 134 /** 135 * @see {@link android.media.Image#getTimestamp} 136 */ 137 @Override 138 public long getTimestamp() { 139 return mImage.getTimestamp(); 140 } 141 142 /** 143 * @see {@link android.media.Image#getWidth} 144 */ 145 @Override 146 public int getWidth() { 147 return mImage.getWidth(); 148 } 149 150 /** 151 * @see {@link android.media.Image#close} 152 */ 153 @Override 154 public void close() { 155 mImage.close(); 156 } 157 158 @Override 159 public String toString() { 160 return Objects.toStringHelper(mImage) 161 .add("timestamp", getTimestamp()) 162 .toString(); 163 } 164} 165