2016-04-06 2 views
6

enter image description hereकैसे एंड्रॉयड

में कैमरा कैप्चरिंग छवि पर लाइव फ़्रेम स्थापित करने के लिए मैं पिक ऊपर की तरह कैमरे पर लाइव फ्रेम सेट करना होगा। लेकिन मेरा setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);getWidth();getHeight();Camera.PictureCallback आदि बहिष्कृत हैं। उपर्युक्त प्राप्त करने के लिए कोई भी कोड जानता है?

मैं

public class CameraOverview1 extends Activity implements SurfaceHolder.Callback{ 
private Camera camera = null; 
private SurfaceView cameraSurfaceView = null; 
private SurfaceHolder cameraSurfaceHolder = null; 
private boolean previewing = false; 
private Display display = null; 
private static int wid = 0, hgt = 0; 
private LayoutInflater layoutInflater = null; 
private View cameraViewControl = null; 
private LayoutParams layoutParamsControl = null; 
private Button btnCapture = null; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

display = getWindowManager().getDefaultDisplay(); 
wid = display.getWidth(); 
hgt = display.getHeight(); 

getWindow().setFormat(PixelFormat.TRANSLUCENT); 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 

setContentView(R.layout.cameraoverlay1); 

cameraSurfaceView = (SurfaceView)findViewById(R.id.cameraSurfaceView); 
cameraSurfaceHolder = cameraSurfaceView.getHolder(); 
cameraSurfaceHolder.addCallback(this); 
cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

layoutInflater = LayoutInflater.from(getBaseContext()); 
layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 

cameraViewControl = layoutInflater.inflate(R.layout.cambutton, null);  
this.addContentView(cameraViewControl, layoutParamsControl); 
btnCapture = (Button)findViewById(R.id.btnCapture); 
btnCapture.setOnClickListener(new OnClickListener() 
{ 
    @Override 
    public void onClick(View v) 
    { 
     // TODO Auto-generated method stub 
     camera.takePicture(cameraShutterCallback, 
       cameraPictureCallbackRaw, 
       cameraPictureCallbackJpeg); 
    } 
}); 
} 
ShutterCallback cameraShutterCallback = new ShutterCallback() 
{ 
@Override 
public void onShutter() 
{ 
    // TODO Auto-generated method stub 
} 
}; 
PictureCallback cameraPictureCallbackRaw = new PictureCallback() 
{ 
@Override 
public void onPictureTaken(byte[] data, Camera camera) 
{ 
    // TODO Auto-generated method stub 
} 
}; 
PictureCallback cameraPictureCallbackJpeg = new PictureCallback() 
{ 
@Override 
public void onPictureTaken(byte[] data, Camera camera) 
{ 
    // TODO Auto-generated method stub 
    Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 

    wid = cameraBitmap.getWidth(); 
    hgt = cameraBitmap.getHeight(); 

    Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888); 

    Canvas canvas = new Canvas(newImage); 

    canvas.drawBitmap(cameraBitmap, 0f, 0f, null); 

    Drawable drawable = getResources().getDrawable 
      (R.drawable.frame1); 
    drawable.setBounds(0, 0, wid, hgt); 
    drawable.draw(canvas); 

    File storagePath = new File(Environment. 
      getExternalStorageDirectory() + "/MyCameraApp/"); 
    storagePath.mkdirs(); 

    File myImage = new File(storagePath, 
      Long.toString(System.currentTimeMillis()) + ".jpg"); 

    try 
    { 
     FileOutputStream out = new FileOutputStream(myImage); 
     newImage.compress(Bitmap.CompressFormat.JPEG, 80, out); 


     out.flush(); 
     out.close(); 
    } 
    catch(FileNotFoundException e) 
    { 
     Log.d("In Saving File", e + "");  
    } 
    catch(IOException e) 
    { 
     Log.d("In Saving File", e + ""); 
    } 

    camera.startPreview(); 

    drawable = null; 

    newImage.recycle(); 
    newImage = null; 

    cameraBitmap.recycle(); 
    cameraBitmap = null; 
} 
}; 
@Override 
public void surfaceChanged(SurfaceHolder holder, 
    int format, int width, int height) 
{ 
// TODO Auto-generated method stub 

if(previewing) 
{ 
    camera.stopPreview(); 
    previewing = false; 
} 
if (camera != null){ 
    try 
    { 
camera.setPreviewDisplay(cameraSurfaceHolder); 
     camera.startPreview(); 
     previewing = true; 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 
@Override 
public void surfaceCreated(SurfaceHolder holder) 
{ 
// TODO Auto-generated method stub 
try 
{ 
    camera = Camera.open(); 
} 
catch(RuntimeException e) 
{ 
    Toast.makeText(getApplicationContext(), "Device camera is not working properly, please try after sometime.", Toast.LENGTH_LONG).show(); 
} 
} 
@Override 
public void surfaceDestroyed(SurfaceHolder holder) 
{ 
// TODO Auto-generated method stub 
camera.stopPreview(); 
camera.release(); 
camera = null; 
previewing = false; 
}} 

वास्तव में इस्तेमाल किया, काफी सब कुछ मान्य नहीं है। कोई विचार?

+0

आपका मतलब एफपीएस या कुल फ्रेम है? –

+0

मैं होर्डिंग फोटो फ्रेम जैसे ऐप का निर्माण कर रहा हूं। उपरोक्त तस्वीर को देखो। –

+0

तो आपको एक डबल पूर्वावलोकन फ्रंट और बैक कैमरा चाहिए? .. –

उत्तर

3

इस नमूने के आधार पर android-Camera2Basic

मैं कुछ परिवर्तन लागू कर दिया गया है और इस परिणाम है:

Camera2BasicFragmentFront.java

Source

Camera2BasicFragment.java

Source

AutoFitTextureView.java

Source

fragment_camera2_basic.xml (com.YourPackage.AutoFitTextureView को बदलने के लिए सुनिश्चित करें)

<?xml version="1.0" encoding="utf-8"?><!-- 
Copyright 2014 The Android Open Source Project 
Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 
    http://www.apache.org/licenses/LICENSE-2.0 
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
--> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.YourPackage.AutoFitTextureView 
     android:id="@+id/texture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" /> 

</RelativeLayout> 

activity_main.xml

<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</FrameLayout> 
<FrameLayout 
android:id="@+id/containerFront" 
android:layout_width="200dp" 
android:layout_height="200dp" /> 
</RelativeLayout> 

MainActivity.java में से:

public class MainActivity extends AppCompatActivity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if (null == savedInstanceState) { 
      getFragmentManager().beginTransaction() 
        .replace(R.id.container, Camera2BasicFragment.newInstance()) 
        .commit(); 
      getFragmentManager().beginTransaction() 
        .replace(R.id.containerFront, Camera2BasicFragmentFront.newInstance()) 
        .commit(); 
     } 
    } 
} 

और इस अनुमति का उपयोग Manifest.xml

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 

करने के लिए सभी का आनंद है कि!

पीएस जावा कक्षाओं में फ़ोटो को सहेजने के लिए विधि भी शामिल है!

+0

क्या आप मुझे पूरा स्रोत कोड दे सकते हैं? मैं सिर्फ एक नौसिखिया हूँ –

+0

यह पूर्ण स्रोत कोड है सर .. –