Giải pháp của tôi cho đến thời điểm này là tạo bố cục nơi chế độ xem chiếm cùng một chế độ xem với chế độ xem lướt sóng.
Sau đó, tôi nhận được giới hạn xem cho xem trước (trong trường hợp của tôi đó là kích thước của màn hình)
//Get our screen size
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
DisplayMetrics metrics = getResources().getDisplayMetrics();
Point size = new Point();
try {
display.getSize(size);
w_width = size.x;
w_height = size.y;
} catch (NoSuchMethodError e) {
w_width = display.getWidth();
w_height = display.getHeight();
}
Sau đó tôi đặt callback là:
callback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
//Get the view bounds to determine how to manipulate this image
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
//If the image is smaller than the screen, don't make the image bigger
int finalWidth = (w_width < options.outWidth) ? w_width : options.outWidth;
int finalHeight = (w_height < options.outHeight) ? w_height : options.outHeight;
int inSampleSize = 1;
options = new BitmapFactory.Options();
if (finalHeight > w_height || finalWidth > w_width) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) finalHeight/(float) w_height);
final int widthRatio = Math.round((float) finalWidth/(float) w_width);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
options.inSampleSize = inSampleSize;
//Decode the smaller image
Bitmap b = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
//Rotate the image correctly
Matrix mat = new Matrix();
mat.postRotate(orientation);
Bitmap newBitmap = Bitmap.createBitmap(b, 0, 0, options.outWidth, options.outHeight, mat, true);
imagePreview.setBackgroundDrawable(new BitmapDrawable(newBitmap));
imagePreview.setVisibility(View.VISIBLE);
}
};
Vì vậy, về cơ bản bề mặt chế độ xem không bao giờ ngừng hiển thị bản xem trước, nhưng lần xem hình ảnh ẩn nó để người dùng không thể nhìn thấy nó.
Sau đó, nếu người dùng quyết định không giữ hình ảnh thì tôi chỉ ẩn lại chế độ xem và SurfaceView sẽ tiếp tục hiển thị bản xem trước trực tiếp. May mắn thay, GS3 dường như không để ý ai đó gọi "camera.startPreview();" ngay cả khi bản xem trước đã diễn ra.
Tôi thực sự không thích câu trả lời này nhưng đó là tất cả những gì tôi có tại thời điểm hoạt động trên thiên hà S3. Nó đã làm việc trên các thiết bị cũ (2.3 trở lên) mà tôi đã thử nhưng nó chắc chắn chỉ là một công việc xung quanh.
Tôi cũng gặp vấn đề này. Bạn đã bao giờ tìm thấy một giải pháp? – BoredAndroidDeveloper
Vẫn không. :( – GeX
Vấn đề này xảy ra với ứng dụng này nếu ai đó cần một ví dụ mã: https://github.com/dawidgatti/dgCam – BoredAndroidDeveloper