0
我的項目是一款相機應用程序,它具有在相機預覽中添加視圖的功能,通常可以使用 。安卓相機預覽黑色
但在某些智能手機上,當視圖添加時,預覽將無任何例外地變成黑色,並且當我粘貼圖片時,圖片是工作的,而不是黑色。 所以我認爲相機對象工作,而不是空或錯誤 我已經嘗試更改預覽大小,我從parameters.getSupportedPreviewSizes()
預覽大小。 我試着將1XXX * 800〜144 * 108設置爲預覽尺寸,但都得到相同的結果...
我該如何解決這個問題?希望得到更好的迴應。由於
void cameraInit() {
try {
bGalleryOpen = false;
mDATA.bFinish = true;
Log.d("cameraInit", "Start");
if (bmPic != null) {
objPic.picRecycled(bmPic);
System.gc();
}
setContentView(R.layout.activity_camera);
RelativeLayout cmain = (RelativeLayout) findViewById(R.id.camera_main);
ibShot = (ImageButton) findViewById(R.id.iBShot);
ibShare = (ImageButton) findViewById(R.id.iBShare);
ibAdd = (ImageButton) findViewById(R.id.iBAdd);
ibGarelly = (ImageButton) findViewById(R.id.iBGarelly);
ibSave = (ImageButton) findViewById(R.id.iBSave);
ibBack = (ImageButton) findViewById(R.id.iBBack);
ibShot.setVisibility(View.VISIBLE);
ibShare.setVisibility(View.GONE);
ibAdd.setVisibility(View.VISIBLE);
ibGarelly.setVisibility(View.VISIBLE);
ibSave.setVisibility(View.GONE);
ibBack.setVisibility(View.VISIBLE);
ibAdd.setOnClickListener(oclAdd);
ibAdd.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ibAdd.setImageResource(R.drawable.cra_007_over);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
ibAdd.setImageResource(R.drawable.cra_007);
}
return false;
}
});
ibShot.setOnClickListener(oclCameraClick);
ibGarelly.setOnClickListener(oclGallery);
ibBack.setOnClickListener(oclBack);
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
mCamera = openCamera();
if (mCamera != null) {
mPreview = new MyPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) preview
.getLayoutParams();
linearParams.height = dm.heightPixels;
linearParams.width = dm.heightPixels/3 * 4;
Log.d("Preview ", "weight:" + linearParams.width + "heigh:"
+ linearParams.height);
linearParams.addRule(RelativeLayout.LEFT_OF, R.id.lTool);
preview.setLayoutParams(linearParams);
preview.addView(mPreview);
}
//Add image view
Bundle bGet = getIntent().getExtras();
if (bGet != null) {
strPicID = bGet.getString("imageID");
if (strPicID != null) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
if (dm.heightPixels != 0) {
lp = new RelativeLayout.LayoutParams(
dm.heightPixels/3 * 4, dm.heightPixels);
}
lp.addRule(RelativeLayout.LEFT_OF, R.id.lTool);
iDrawble = this.getResources().getIdentifier(strPicID,
"drawable", this.getPackageName());
Options bo = new Options();
bo.inJustDecodeBounds = true;
bo.inSampleSize = 1;
bo.inJustDecodeBounds = false;
InputStream inputStream = this.getResources()
.openRawResource(iDrawble);
Bitmap bm = BitmapFactory.decodeStream(inputStream, null,
bo);
myView = new PictureView(this, bm);
mDATA.alPicView.clear();
mDATA.alPicView.add(myView);
for (int i = 0; i < mDATA.alPicView.size(); i++) {
Log.d(TAG,
"All " + String.valueOf(mDATA.alPicView.size())
+ " View");
cmain.addView(mDATA.alPicView.get(i), lp);
Log.d(TAG, "Add " + String.valueOf(i + 1) + " View");
}
mDATA.bFinish = false;
getIntent().removeExtra("imageSrc");
getIntent().removeExtra("imageID");
}
}
} catch (Exception errMsg) {
errMsg.printStackTrace();
}
}
和mypreview代碼
public class MyPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
String TAG = "myPreview";
Size sPic;
public MyPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Log.d(TAG, "CameraPreview");
}
public void surfaceCreated(SurfaceHolder holder) {
try {
Log.d(TAG, "CameraPreview:surfaceCreated");
if (mCamera != null) {
mCamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Log.d(TAG, "onPreviewFrame: preview: data=" + data);
}
});
mCamera.setOneShotPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Log.d(TAG, "onPreviewFrame: short preview: data="
+ data);
}
});
mCamera.setErrorCallback(new ErrorCallback() {
public void onError(int error, Camera camera) {
Log.d(TAG, "onError: error=" + error);
}
});
}
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("surfaceCreated",
"Error setting camera preview: " + e.getMessage());
mCamera.release();
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your
// activity.
Log.d(TAG, "CameraPreview:surfaceDestroyed");
this.getHolder().removeCallback(this);
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.d(TAG, "CameraPreview:surfaceChanged W:" + w + ",H:" + h);
if (mHolder.getSurface() == null) {
Log.d(TAG, "CameraPreview:getSurface Null");
// preview surface does not exist
return;
}
// stop preview before making changes
try {
Log.d(TAG, "CameraPreview:surfaceChanged Stop");
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
e.printStackTrace();
}
// start preview with new settings
try {
// set preview size and make any resize, rotate or
Camera.Parameters params = mCamera.getParameters();
Camera.Size size = getBestPreviewSize(params, w, h);
if (size != null) {
Log.d(TAG, "CameraPreview:surfaceChanged get preSize:"
+ size.width + "," + size.height);
Size sPic = getBestPhotoSize(params, size.width, size.height);
if (size != null) {
mDATA.m_Size = size;
params.setPreviewSize(size.width, size.height);
params.setPictureSize(sPic.width, sPic.height);
}
Log.d(TAG,
"CameraPreview:surfaceChanged preSize:"
+ params.getPreviewSize().width + ","
+ params.getPreviewSize().height);
Log.d(TAG,
"CameraPreview:surfaceChanged picSize:"
+ params.getPictureSize().width + ","
+ params.getPictureSize().height);
mCamera.setParameters(params);
}
mCamera.startPreview();
Log.d(TAG, "CameraPreview:surfaceChanged Start");
} catch (Exception e) {
// Log.d(TAG, "Error starting camera preview: " +
e.printStackTrace();
mCamera.release();
mCamera = null;
}
}
public Camera getCamera() {
return mCamera;
}
public Camera.Size getBestPreviewSize(Camera.Parameters parameters, int w,
int h) {
Camera.Size result = null;
float fRatio = 3/4;
Log.d(TAG, "CameraPreview:getBestPreviewSize");
try {
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
Log.d("getBestPreviewSize", "suport w:" + size.width + "h:"
+ size.height);
if (size.width <= w && size.height <= h && (h/w) == fRatio) {
if (null == result)
result = size;
else {
int resultDelta = w - result.width + h - result.height;
int newDelta = w - size.width + h - size.height;
if (newDelta < resultDelta)
result = size;
}
}
}
} catch (Exception errMsg) {
errMsg.printStackTrace();
}
return result;
}
public Size getBestPhotoSize(Parameters parameters, int w, int h) {
Size result = null;
float fRatio = h/w;
Log.d(TAG, "CameraPreview:getBestPreviewSize");
try {
for (Camera.Size size : parameters.getSupportedPictureSizes()) {
Log.d("getBestPhotoSize", "suport w:" + size.width + "h:"
+ size.height);
float fTmp = size.height/size.width;
if (fTmp == fRatio && size.width <= 1800) {
if (null == result)
result = size;
else {
int resultDelta = w - result.width + h - result.height;
int newDelta = w - size.width + h - size.height;
if (newDelta < resultDelta)
result = size;
}
}
}
} catch (Exception errMsg) {
errMsg.printStackTrace();
}
return result;
}
}
一些代碼,如果你不介意? – Skynet
ok.I編輯我的問題內容,功能代碼在~~ – user2724022
*在某些智能手機上添加視圖時,預覽會變成黑色* - 哪種設備/設備(錯誤)以這種方式表現? –